在python里使用await關(guān)鍵字來等另外一個協(xié)程的實例
一個協(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 outer
waiting for result1
in phase1
waiting for result2
in phase2
return 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_condition consumer 2 is waiting consumer 3 is waiting consumer 4 is waiting consumer 1 is waiting consumer 0 is waiting notifying 1 consumers consumer 2 triggered ending consumer 2 notifying 2 consumers consumer 3 triggered ending consumer 3 consumer 4 triggered ending consumer 4 notifying remaining consumers ending manipulate_condition consumer 1 triggered ending consumer 1 consumer 0 triggered ending consumer 0
以上這篇在python里使用await關(guān)鍵字來等另外一個協(xié)程的實例就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Python編程實戰(zhàn)之Oracle數(shù)據(jù)庫操作示例
這篇文章主要介紹了Python編程實戰(zhàn)之Oracle數(shù)據(jù)庫操作,結(jié)合具體實例形式分析了Python的Oracle數(shù)據(jù)庫模塊cx_Oracle包安裝、Oracle連接及操作技巧,需要的朋友可以參考下2017-06-06
Python爬取網(wǎng)頁的所有內(nèi)外鏈的代碼
這篇文章主要介紹了Python爬取網(wǎng)頁的所有內(nèi)外鏈,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-04-04
python調(diào)用動態(tài)鏈接庫的基本過程詳解
這篇文章主要介紹了python調(diào)用動態(tài)鏈接庫的基本過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06
python操作csv格式文件之csv.DictReader()方法
這篇文章主要介紹了python操作csv格式文件之csv.DictReader()方法,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價值,感興趣的小伙伴可以參考一下2022-06-06
windows10環(huán)境下用anaconda和VScode配置的圖文教程
這篇文章主要介紹了windows10環(huán)境下用anaconda和VScode配置的圖文教程,本文通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家介紹的非常詳細(xì),需要的朋友可以參考下2020-03-03

