---
title: 关于Stream间任务的同步等待（通过Notify实现）
description: "多Stream之间任务的同步等待可以利用Notify实现，例如，若stream2的任务依赖stream1的任务，想保证stream1中的任务先完成，这时可创建一个Notify，调用acl.rt.record_notify(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_1048.html)接口将Notify插入到stream1中（通常称为Record Notify任务），调用acl.rt.wait_and_reset_notify(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_1049.html)接口在stream2中插入一个等待Notify完成的任务（通常称为Wait Notify任务）。"
url: https://www.hiascend.com/document/detail/zh/canncommercial/latest/programug/acldevg/aclpythondevg_0101.html
sourcePath: /source/zh/canncommercial/900/programug/acldevg/aclpythondevg_0101.html
indexId: a39c98296f4fad76a56a66038c042960fdba5d8df0495a4190ec5ef30982d5b771
---
# 关于Stream间任务的同步等待（通过Notify实现）

多Stream之间任务的同步等待可以利用Notify实现，例如，若stream2的任务依赖stream1的任务，想保证stream1中的任务先完成，这时可创建一个Notify，调用acl.rt.record_notify(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_1048.html)接口将Notify插入到stream1中（通常称为Record Notify任务），调用acl.rt.wait_and_reset_notify(https://www.hiascend.comdocument/detail/zh/canncommercial/900/API/runtimeapi/aclpythondevg_01_1049.html)接口在stream2中插入一个等待Notify完成的任务（通常称为Wait Notify任务）。

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

上图中的模型加载与执行的流程请参见模型管理，算子加载与执行的流程请参见单算子调用。

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

```
import acl
# ......
# 创建一个notify
notify, ret = acl.rt.create_notify(0)

# 创建stream_1、stream_2
stream_1, ret = acl.rt.create_stream()
stream_2, ret = acl.rt.create_stream()

# 在stream_1末尾添加了一个notify
ret = acl.rt.record_notify(notify, stream_1)

# 阻塞stream_2运行，直到指定notify发生，也就是stream_1执行完成
# stream_1完成后，唤醒stream_2，继续执行stream_2的任务
ret = acl.rt.wait_and_reset_notify(notify, stream_2, 0)

ret = acl.rt.synchronize_stream(stream_1)
ret = acl.rt.synchronize_stream(stream_2)

# 显式销毁资源
ret = acl.rt.destroy_stream(stream_2)
ret = acl.rt.destroy_stream(stream_1)
ret = acl.rt.destroy_notify(notify)
# ......
```
