在python里使用await关键字来等另外一个协程的实例
作者:caimouse 时间:2021-03-01 19:10:16
一个协程里可以启动另外一个协程,并等待它完成返回结果,采用await关键字,
例子如下:
import asyncio
async def outer():
print('in outer')
print('waiting for result1')
result1 = await phase1()
print('waiting for result2')
result2 = await phase2(result1)
return (result1, result2)
async def phase1():
print('in phase1')
return 'result1'
async def phase2(arg):
print('in phase2')
return 'result2 derived from {}'.format(arg)
event_loop = asyncio.get_event_loop()
try:
return_value = event_loop.run_until_complete(outer())
print('return value: {!r}'.format(return_value))
finally:
event_loop.close()
输出结果如下:
in outer
waiting for result1
in phase1
waiting for result2
in phase2
return value: ('result1', 'result2 derived from result1')
await关键字添加了一个新的协程到循环里,而不需要明确地添加协程到这个事件循环里。
补充知识:python里使用Condition对象来唤醒指定数量的协程
在asyncio库里,定义Condition对象,它的行为与事件Event有点像,区别是事件是通知所有对象,Condition对象可以指定一定数量的协程被通知,它是通过函数notify()来实现的,如果参数里放2,就是通知两个协程,例子如下:
import asyncio
async def consumer(condition, n):
with await condition:
print('consumer {} is waiting'.format(n))
await condition.wait()
print('consumer {} triggered'.format(n))
print('ending consumer {}'.format(n))
async def manipulate_condition(condition):
print('starting manipulate_condition')
# pause to let consumers start
await asyncio.sleep(0.1)
for i in range(1, 3):
with await condition:
print('notifying {} consumers'.format(i))
condition.notify(n=i)
await asyncio.sleep(0.1)
with await condition:
print('notifying remaining consumers')
condition.notify_all()
print('ending manipulate_condition')
async def main(loop):
# Create a condition
condition = asyncio.Condition()
# Set up tasks watching the condition
consumers = [
consumer(condition, i)
for i in range(5)
]
# Schedule a task to manipulate the condition variable
loop.create_task(manipulate_condition(condition))
# Wait for the consumers to be done
await asyncio.wait(consumers)
event_loop = asyncio.get_event_loop()
try:
result = event_loop.run_until_complete(main(event_loop))
finally:
event_loop.close()
结果输出如下:
starting manipulate_condition
consumer 2 is waiting
consumer 3 is waiting
consumer 4 is waiting
consumer 1 is waiting
consumer 0 is waiting
notifying 1 consumers
consumer 2 triggered
ending consumer 2
notifying 2 consumers
consumer 3 triggered
ending consumer 3
consumer 4 triggered
ending consumer 4
notifying remaining consumers
ending manipulate_condition
consumer 1 triggered
ending consumer 1
consumer 0 triggered
ending consumer 0
来源:https://blog.csdn.net/caimouse/article/details/77867169
标签:python,await,关键字,协程
0
投稿
猜你喜欢
Pytorch反向求导更新网络参数的方法
2021-02-07 11:48:52
Python 中的with关键字使用详解
2022-10-03 08:25:25
js实现的捐赠管理完整实例
2023-08-22 05:25:14
python中setuptools的作用是什么
2022-01-31 07:19:26
Python 统计Jira的bug 并发送邮件功能
2021-03-24 05:44:24
php中常量DIRECTORY_SEPARATOR用法深入分析
2023-09-06 00:23:27
Dreamweaver使用中的7个常见问题与解答
2007-11-03 11:34:00
php7 参数、整形及字符串处理机制修改实例分析
2023-11-23 19:48:31
CSS网页布局编码小技巧整理
2009-12-30 16:50:00
SQL Server数据库连接中常见的错误分析
2009-01-15 12:51:00
python中xlwt模块的具体用法
2023-11-08 07:10:15
如何判断JavaScript变量的类型
2009-02-25 12:28:00
python使用Thread的setDaemon启动后台线程教程
2023-09-29 05:49:26
使用Python手工计算x的算数平方根,来自中国古人的数学智慧
2021-12-07 01:29:53
Access数据库安全问答
2007-08-23 15:28:00
tensorflow使用L2 regularization正则化修正overfitting过拟合方式
2023-11-26 04:07:32
sql server海量数据库的查询优化及分页算法方案
2010-07-02 21:17:00
python Django模板的使用方法
2021-06-09 15:29:14
Python3实现的回文数判断及罗马数字转整数算法示例
2023-07-07 05:10:06
Python装饰器使用示例及实际应用例子
2022-01-16 01:59:32