---
title: 关于Stream间任务的同步等待（通过Event实现）
description: "多Stream之间任务的同步等待可以利用Event实现，调用acl.rt.stream_wait_event(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0101.html)接口阻塞指定Stream的运行，直到指定的Event完成。需在调用acl.rt.stream_wait_event(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0101.html)接口前，先调用acl.rt.record_event(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0093.html)接口。"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/programug/acldevg/aclpythondevg_0026.html
sourcePath: /source/zh/canncommercial/900/programug/acldevg/aclpythondevg_0026.html
indexId: 69de257e15a5230174a73148be8c06b3e6e6978839f651ff9af73ef4ac24af3c71
---
# 关于Stream间任务的同步等待（通过Event实现）

多Stream之间任务的同步等待可以利用Event实现，调用acl.rt.stream_wait_event(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0101.html)接口阻塞指定Stream的运行，直到指定的Event完成。需在调用acl.rt.stream_wait_event(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0101.html)接口前，先调用acl.rt.record_event(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_0093.html)接口。

模型加载与执行的流程请参见模型管理。

算子加载与执行的流程请参见单算子调用。

图1 同步等待流程_多Stream场景

调用接口后，需增加异常处理的分支，并记录报错日志、提示日志，此处不一一列举。以下是关键步骤的代码示例，不可以直接拷贝运行，仅供参考。

```
import acl
# ......
# 创建一个Event。
event, ret = acl.rt.create_event()

# 创建两个Stream。
s1, ret = acl.rt.create_stream()
s2, ret = acl.rt.create_stream()

# 在s1末尾添加了一个event。
ret = acl.rt.record_event(event, s1)

# 阻塞s2运行，直到指定Event发生，也就是s1执行完成。
# s1完成后，唤醒s2，继续执行s2的任务。
ret = acl.rt.stream_wait_event(s2, event)

ret = acl.rt.synchronize_stream(s1)
ret = acl.rt.synchronize_stream(s2)

# 显式销毁资源。
ret = acl.rt.destroy_stream(s2)
ret = acl.rt.destroy_stream(s1)
ret = acl.rt.destroy_event(event)
# ......
```
