在python里使用await关键字来等另外一个协程的实例
Python  /  管理员 发布于 7年前   563
一个协程里可以启动另外一个协程,并等待它完成返回结果,采用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 outerwaiting for result1in phase1waiting for result2in phase2return 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_conditionconsumer 2 is waitingconsumer 3 is waitingconsumer 4 is waitingconsumer 1 is waitingconsumer 0 is waitingnotifying 1 consumersconsumer 2 triggeredending consumer 2notifying 2 consumersconsumer 3 triggeredending consumer 3consumer 4 triggeredending consumer 4notifying remaining consumersending manipulate_conditionconsumer 1 triggeredending consumer 1consumer 0 triggeredending consumer 0
以上这篇在python里使用await关键字来等另外一个协程的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。
122 在
学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..123 在
Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..原梓番博客 在
在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..博主 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..1111 在
佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
			Copyright·© 2019 侯体宗版权所有·
			粤ICP备20027696号
			 
			
