python中利用隊列asyncio.Queue進行通訊詳解
前言
本文主要給大家介紹了關(guān)于python用隊列asyncio.Queue通訊的相關(guān)內(nèi)容,分享出來供大家參考學習,下面話不多說了,來一起看看詳細的介紹吧。
asyncio.Queue與其它隊列是一樣的,都是先進先出,它是為協(xié)程定義的
例子如下:
import asyncio
async def consumer(n, q):
print('consumer {}: starting'.format(n))
while True:
print('consumer {}: waiting for item'.format(n))
item = await q.get()
print('consumer {}: has item {}'.format(n, item))
if item is None:
# None is the signal to stop.
q.task_done()
break
else:
await asyncio.sleep(0.01 * item)
q.task_done()
print('consumer {}: ending'.format(n))
async def producer(q, num_workers):
print('producer: starting')
# Add some numbers to the queue to simulate jobs
for i in range(num_workers * 3):
await q.put(i)
print('producer: added task {} to the queue'.format(i))
# Add None entries in the queue
# to signal the consumers to exit
print('producer: adding stop signals to the queue')
for i in range(num_workers):
await q.put(None)
print('producer: waiting for queue to empty')
await q.join()
print('producer: ending')
async def main(loop, num_consumers):
# Create the queue with a fixed size so the producer
# will block until the consumers pull some items out.
q = asyncio.Queue(maxsize=num_consumers)
# Scheduled the consumer tasks.
consumers = [
loop.create_task(consumer(i, q))
for i in range(num_consumers)
]
# Schedule the producer task.
prod = loop.create_task(producer(q, num_consumers))
# Wait for all of the coroutines to finish.
await asyncio.wait(consumers + [prod])
event_loop = asyncio.get_event_loop()
try:
event_loop.run_until_complete(main(event_loop, 2))
finally:
event_loop.close()
輸出如下:
consumer 0: starting consumer 0: waiting for item consumer 1: starting consumer 1: waiting for item producer: starting producer: added task 0 to the queue producer: added task 1 to the queue consumer 0: has item 0 consumer 1: has item 1 producer: added task 2 to the queue producer: added task 3 to the queue consumer 0: waiting for item consumer 0: has item 2 producer: added task 4 to the queue consumer 1: waiting for item consumer 1: has item 3 producer: added task 5 to the queue producer: adding stop signals to the queue consumer 0: waiting for item consumer 0: has item 4 consumer 1: waiting for item consumer 1: has item 5 producer: waiting for queue to empty consumer 0: waiting for item consumer 0: has item None consumer 0: ending consumer 1: waiting for item consumer 1: has item None consumer 1: ending producer: ending
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
相關(guān)文章
一個基于flask的web應用誕生 組織結(jié)構(gòu)調(diào)整(7)
一個基于flask的web應用誕生第七篇,這篇文章主要介紹了組織結(jié)構(gòu)的調(diào)整,具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-04-04
Python對list列表結(jié)構(gòu)中的值進行去重的方法總結(jié)
這篇文章主要介紹了Python對列表list中的值進行去重的方法總結(jié),文中給出的方法都能保持去重后的順序不發(fā)生改變,需要的朋友可以參考下2016-05-05
解決Python的str強轉(zhuǎn)int時遇到的問題
下面小編就為大家分享一篇解決Python的str強轉(zhuǎn)int時遇到的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-04-04
Python PyTorch 如何獲取 MNIST 數(shù)據(jù)
這篇文章主要介紹了Python PyTorch 如何獲取 MNIST 數(shù)據(jù),通過示例代碼介紹了PyTorch 保存 MNIST 數(shù)據(jù),PyTorch 顯示 MNIST 數(shù)據(jù)的操作方法,感興趣的朋友跟隨小編一起看看吧2024-04-04
Python通過matplotlib畫雙層餅圖及環(huán)形圖簡單示例
這篇文章主要介紹了Python通過matplotlib畫雙層餅圖及環(huán)形圖簡單示例,具有一定借鑒價值,需要的朋友可以參考下。2017-12-12
keras:model.compile損失函數(shù)的用法
這篇文章主要介紹了keras:model.compile損失函數(shù)的用法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07

