---
title: 函数：execute_async
description: "执行模型推理，异步接口。"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/API/ascendgraphapi/aclpythondevg_01_0147.html
sourcePath: /source/zh/canncommercial/900/API/ascendgraphapi/aclpythondevg_01_0147.html
indexId: 148dff42afc552b6086cdf800ce77530895582206f7b2166dcd12f739e01442575
---
# 函数：execute_async

#### 功能说明

执行模型推理，异步接口。


#### 函数原型

- C函数原型
  1 aclError aclmdlExecuteAsync(uint32_t modelId, const aclmdlDataset *input, aclmdlDataset *output, aclrtStream stream)

- python函数
  1 ret  = acl.mdl.execute_async(model_id, input, output, stream)


#### 参数说明

| 参数名 | 说明 |
| --- | --- |
| model\_id | int，指定需要执行推理的模型的ID。 可在以下接口成功加载模型后获取到模型ID。 acl.mdl.load\_from\_file acl.mdl.load\_from\_mem acl.mdl.load\_from\_file\_with\_mem acl.mdl.load\_from\_mem\_with\_mem |
| input | int，模型推理的输入数据的指针地址，具体请参见aclmdlDataset。 |
| output | int，模型推理的输出数据的指针地址，具体请参见aclmdlDataset。 |
| stream | int，指定已创建的Stream指针地址，如需指定新的Stream，可通过acl.rt.create\_stream(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg\_01\_0079.html)等接口创建并获取Stream指针地址。 |


#### 返回值说明

| 返回值 | 说明 |
| --- | --- |
| ret | int，错误码，返回0表示成功，返回其它值表示失败。 |


#### 约束说明

- 该接口是异步接口，调用接口成功仅表示任务下发成功，不表示任务执行成功。调用该接口后，需调用同步等待接口（例如，  acl.rt.synchronize_stream(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0087.html)
）确保任务已执行完成。
- 对同一个“model_id”的模型，不可通过acl.mdl.execute_async接口执行多Stream并发场景下的模型推理。
- 如需在多线程中使用同一个“model_id”，则用户线程间需加锁，保证刷新输入输出内存、保证执行是连续操作，例如：

```
// 线程A的接口调用顺序：
lock(handle1) -> acl.rt.memcpy_async(stream1)刷新输入输出内存 -> acl.mdl.execute_async(modelId1,stream1)执行推理 -> unlock(handle1)
// 线程B的接口调用顺序：
lock(handle1) -> acl.rt.memcpy_async(stream1)刷新输入输出内存 -> acl.mdl.execute_async(modelId1,stream1)执行推理 -> unlock(handle1)
```


- 模型加载、模型执行、模型卸载的操作必须在同一个Context下（关于Context的创建请参见  acl.rt.create_context(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0072.html)
）。
- 存放模型输入/输出数据的内存，可以使用以下接口申请：  acl.rt.malloc(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0110.html)
、  acl.rt.malloc_host(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0116.html)
、  acl.rt.malloc_cached(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0111.html)
接口、  acl.media.dvpp_malloc(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/dvpp/aclpythondevg_01_0223.html)
接口或  acl.himpi.dvpp_malloc(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/dvpp/aclpythondevg_01_0416.html)
接口。
  其中，acl.media.dvpp_malloc(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/dvpp/aclpythondevg_01_0223.html)接口和acl.himpi.dvpp_malloc(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/dvpp/aclpythondevg_01_0416.html)接口是媒体数据处理功能专用的内存申请接口，一般从性能角度，为了减少拷贝，媒体数据处理的输出作为模型推理的输入，实现内存复用。

- 但由于媒体数据处理访问的地址空间有限，为确保媒体数据处理时内存足够，除媒体数据处理功能外的其它功能（例如，模型加载），建议调用其它接口申请内存，例如  acl.rt.malloc(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0110.html)
接口。


#### 资源参考

- 接口调用流程，参见  接口调用流程(https://www.hiascend.comdocument/detail/zh/canncommercial/900/programug/acldevg/aclpythondevg_0037.html)
。
- 接口调用示例，参见  示例代码(https://www.hiascend.comdocument/detail/zh/canncommercial/900/programug/acldevg/aclpythondevg_0038.html)
。
- Stream创建与管理，请参见  Stream管理(https://www.hiascend.comdocument/detail/zh/canncommercial/900/programug/acldevg/aclpythondevg_0016.html)
。
