一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

腳本之家,腳本語言編程技術(shù)及教程分享平臺(tái)!
分類導(dǎo)航

Python|VBS|Ruby|Lua|perl|VBA|Golang|PowerShell|Erlang|autoit|Dos|bat|

服務(wù)器之家 - 腳本之家 - Python - python中利用隊(duì)列asyncio.Queue進(jìn)行通訊詳解

python中利用隊(duì)列asyncio.Queue進(jìn)行通訊詳解

2020-12-07 00:15caimouse Python

asyncio是Python 3.4版本引入的標(biāo)準(zhǔn)庫,直接內(nèi)置了對(duì)異步IO的支持。 下面這篇文章主要給大家介紹了關(guān)于python中利用隊(duì)列asyncio.Queue進(jìn)行通訊的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。

前言

本文主要給大家介紹了關(guān)于python隊(duì)列asyncio.Queue通訊的相關(guān)內(nèi)容,分享出來供大家參考學(xué)習(xí),下面話不多說了,來一起看看詳細(xì)的介紹吧。

asyncio.Queue與其它隊(duì)列是一樣的,都是先進(jìn)先出,它是為協(xié)程定義的

例子如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
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()

輸出如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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é)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)服務(wù)器之家的支持。

原文鏈接:http://blog.csdn.net/caimouse/article/details/77916974

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 嫩草研究 | 99久久免费国产香蕉麻豆 | www.99精品视频在线播放 | 美女林柏欣21p人体之仓之梦 | 色播艾小青国产专区在线播放 | 色综七七久久成人影 | 麻豆找网服 | 极品一区 | 99热在线观看免费 | 性俄罗斯xxxxxhd | 欧美老人与小伙子性生交 | 午夜在线观看免费观看 视频 | 男神插曲女生动漫完整版动漫 | 69热视频| 日韩精品一区二区三区中文在线 | 任你操视频在线观看 | 国产在线精品香蕉综合网一区 | 四虎免费影院4hu永久免费 | 日韩成片| 精品视频一区二区 | 午夜爱爱片 | 水多多凹凸福利视频导航 | 亚洲男人天堂影院 | 欧美一级视 | acg火影忍者熟密姬纲手h | 教师波多野结衣在线播放 | 亚洲精品视频免费在线观看 | 丰满在线观看 | japanesemoms乱熟| 精品国产一区二区三区久久久蜜臀 | 国产亚洲综合久久 | 成人免费在线视频观看 | 久久青青草原精品国产软件 | 午夜精品久久久久久久2023 | 欧美国产日产精品免费视频 | 国产精品 色 | 手机看片国产免费现在观看 | x8x8国产在线观看2021 | 国产精品久久久久久久久久久搜索 | 调教全程肉动画片在线观看 | 人人最怕九月羊 |