亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

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

在python里使用await關(guān)鍵字來(lái)等另外一個(gè)協(xié)程的實(shí)例

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

一個(gè)協(xié)程里可以啟動(dòng)另外一個(gè)協(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)鍵字添加了一個(gè)新的協(xié)程到循環(huán)里,而不需要明確地添加協(xié)程到這個(gè)事件循環(huán)里。

補(bǔ)充知識(shí):python里使用Condition對(duì)象來(lái)喚醒指定數(shù)量的協(xié)程

在asyncio庫(kù)里,定義Condition對(duì)象,它的行為與事件Event有點(diǎn)像,區(qū)別是事件是通知所有對(duì)象,Condition對(duì)象可以指定一定數(shù)量的協(xié)程被通知,它是通過(guò)函數(shù)notify()來(lái)實(shí)現(xiàn)的,如果參數(shù)里放2,就是通知兩個(gè)協(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)鍵字來(lái)等另外一個(gè)協(xié)程的實(shí)例就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持好吧啦網(wǎng)。

標(biāo)簽: Python 編程
相關(guān)文章:
主站蜘蛛池模板: 国产成人18黄网站麻豆 | 有人有片的观看免费视频 | 亚洲色图男人天堂 | 高清不卡日本v在线二区 | 女神穿上情趣丝袜啪啪一整晚 | 精品无码一区在线观看 | 国内精品福利 | 免费又爽又黄的禁片1000部 | 成人理论 | 欧美日韩在线视频免费完整 | 成人综合在线视频 | 黄色短视频免费观看 | 亚洲一区精品在线 | 香蕉免费网站 | 在线视频 自拍 | 欧美成人免费网在线观看 | 男女爱爱小视频在线观看 | 久碰香蕉精品视频在线观看 | 免费特级黄毛片在线成人观看 | 好骚综合在线 | www.自拍| 欧美精品国产日韩综合在线 | 91在线视频免费91 | 伊甸园精品视频网站 | 国产专区视频在线观看 | 国产精品久久在线观看 | 日本乱中文字幕系列 | 国内精品视频一区二区八戒 | 欧美黑人两根巨大挤入 | 免费三级网址 | 日日噜噜夜夜狠狠tv视频免费 | 国产真实女人一级毛片 | 欧美亚洲精品在线 | 日韩爆操| 亚洲精品一区在线观看 | 国产大片线上免费观看 | 国产精品日韩 | 丁香六月纪婷婷激情综合 | 久久久久久久久综合影视网 | 国产剧情第一页 | 在线国产资源 |