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

您的位置:首頁技術(shù)文章
文章詳情頁

在python里使用await關(guān)鍵字來等另外一個協(xié)程的實例

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

一個協(xié)程里可以啟動另外一個協(xié)程,并等待它完成返回結(jié)果,采用await關(guān)鍵字,

例子如下:

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()輸出結(jié)果如下:in outerwaiting for result1in phase1waiting for result2in phase2return value: (’result1’, ’result2 derived from result1’)

await關(guān)鍵字添加了一個新的協(xié)程到循環(huán)里,而不需要明確地添加協(xié)程到這個事件循環(huán)里。

補充知識:python里使用Condition對象來喚醒指定數(shù)量的協(xié)程

在asyncio庫里,定義Condition對象,它的行為與事件Event有點像,區(qū)別是事件是通知所有對象,Condition對象可以指定一定數(shù)量的協(xié)程被通知,它是通過函數(shù)notify()來實現(xiàn)的,如果參數(shù)里放2,就是通知兩個協(xié)程,例子如下:

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()

結(jié)果輸出如下:

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關(guān)鍵字來等另外一個協(xié)程的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。

標簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 在线视频第一页 | 久久青青草视频 | 性做久久久久久免费观看 | 最近手机高清中文字幕大全7 | 日韩在线国产 | 亚洲欧美日韩在线线精品 | 国产免费一级精品视频 | 亚洲综合爱久久影院 | 国产成人精品午夜 | 玖玖香蕉视频 | 97在线视频免费观看费观看 | 日本又黄又爽又免费 | 中文字幕播放 | 亚洲女人网 | 伊人色综合久久天天人手人停 | 久草视频资源 | 67194欧美成l人在线观看免费 | 毛片免费全部免费观看 | 欧美一级毛片在线看视频 | 一及毛片 | 欧美日韩一级片在线观看 | 午夜一级毛片免费视频 | 国产99视频精品免视看7 | 亚洲天堂久久精品成人 | 亚洲精品一区二区四季 | 黄色三级理沦片 | 久久久亚洲欧洲日产国码二区 | 草久久久久 | 在线免费精品视频 | 国产三级精品三级 | 猛操美女| 成 人 在 线 免费 8888 www | 日韩在线成人 | 成人在线观看网址 | 欧美三级aaa | 91久国产在线观看 | 欧美国产高清 | 99久久久国产精品免费播放器 | 福利视频在线午夜老司机 | 手机毛片免费看 | 欧美人交性视频在线香蕉 |