国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

在python里使用await關鍵字來等另外一個協程的實例

瀏覽:4日期:2022-07-26 13:59:02

一個協程里可以啟動另外一個協程,并等待它完成返回結果,采用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關鍵字來等另外一個協程的實例就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持好吧啦網。

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 中日韩欧美一级毛片 | 亚洲精品美女 | 在线免费公开视频 | 91青草久久久久久清纯 | 国产91成人精品亚洲精品 | 日本三级成人午夜视频网 | 久久骚| 100000免费啪啪18免进 | 国产在线观a免费观看 | 美女扒开腿让男人桶个爽 | 日本香蕉一区二区三区 | 亚洲精品国产美女在线观看 | 亚洲欧美中文日韩在线v日本 | 国产精品v欧美精品v日本精 | 日本午夜精品 | 一级毛片视频播放 | 亚洲综合中文 | 国模在线播放 | 91成人免费观看在线观看 | 国产欧美久久久另类精品 | 成人高清毛片a | 永久免费毛片手机版在线看 | 欧美日韩性视频一区二区三区 | 一色屋色费精品视频在线观看 | a三级黄色片 | 欧美free性秘书xxxxhd | 国产成人精品日本亚洲网站 | 日韩国产成人精品视频 | 国产精品午夜国产小视频 | 久久久久久久99久久久毒国产 | 成人自拍网 | 亚洲免费观看视频 | 手机看片午夜 | 偷拍第1页 | 久久久久久一级毛片免费无遮挡 | 欧美午夜激情影院 | 99精品国产成人一区二区在线 | 在线视频 一区二区 | 在线看片 在线播放 | 亚洲免费在线观看视频 | 国产成人午夜性a一级毛片 国产成人午夜性视频影院 国产成人香蕉久久久久 |