色综合图-色综合图片-色综合图片二区150p-色综合图区-玖玖国产精品视频-玖玖香蕉视频

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

在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福利国产在线观一区二区 | 国产成人高清精品免费观看 | 免费看v片网站 | 亚洲天堂久久久 | 欧美精品做人一级爱免费 | 一级白嫩美女毛片免费 | 精品国产夜色在线 | 国产做a爰片久久毛片a | 成人做爰毛片免费视频 | 国产精品拍自在线观看 | 成人免费毛片一区二区三区 | 午夜三级网 | 欧美白人最猛性xxxxx | 欧美性一区二区三区 | 办公室紧身裙丝袜av在线 | 国产精品久久久久久久久久日本 | 亚洲精品高清国产麻豆专区 | 51国产偷自视频区视频手机播器 | 成人国产精品免费网站 | 久久国产视频网站 | 一级生性活免费视频 | 9cao视频精品 | 色综合久久综合 | 久久久久久久久久久观看 | 国产精品成人观看视频网站 | 美女扒开腿让男人桶尿口 | 亚洲综合国产一区在线 | 精品视自拍视频在线观看 | 色站综合 | 在线看片中文字幕 | 亚洲va精品中文字幕动漫 | 国产一级特黄aa级特黄裸毛片 | 国产成人综合高清在线观看 | 中文乱码字幕午夜无线观看 | 国产精品18久久久久久vr | 一级中国乱子伦视频 | 青青影院一区二区免费视频 |