京公网安备 11010802034615号
经营许可证编号:京B2-20210330
python中利用队列asyncio.Queue进行通讯详解
本文主要给大家介绍了关于python用队列asyncio.Queue通讯的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。

asyncio.Queue与其它队列是一样的,都是先进先出,它是为协程定义的
例子如下:
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
总结
以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值.
数据分析咨询请扫描二维码
若不方便扫码,搜微信号:CDAshujufenxi
CDA数据分析师 出品 作者:李诗怡 1. 用户标签体系 定义: 通过一系列高度精炼的特征标识,对用户属性、行为与偏好进行量化刻画 ...
2026-09-24Pandas是Python生态中用于表格数据处理的核心库,广泛应用于数据清洗、统计运算、报表输出、数据分析建模等场景。在处理极大数值 ...
2026-09-24随着数字经济快速发展,数据已成为核心生产要素,各行各业的业务沉淀、用户行为、设备运行、市场交易均产生海量数据。数据处理作 ...
2026-09-24 很多分析师每天和数据打交道,但当被问到“标签是什么”“标签和指标有什么区别”“标签体系如何设计”时,却常常答不上来。 ...
2026-09-24在时序数据分析中,大部分业务数据并非持续平稳变化,而是会在某些时间节点出现突然抬升、断崖下跌、趋势反转、波动异变等现象, ...
2026-09-23在统计学与数据分析中,研究多组数据差异最常用的方法为单因素方差分析与事后多重比较。很多数据分析初学者容易混淆两者功能,认 ...
2026-09-23 很多数据分析师每天都在写 SQL,但当被问到“DQL 的本质是什么”“SELECT 子句的书写顺序与执行顺序为何不同”“INNER JOIN ...
2026-09-23 很多数据分析师写过无数个SELECT查询,但当被问到“如何新建一张表来固化中间数据”“创建视图和创建物理表有什么区别”“视 ...
2026-09-22CDA数据分析师 出品 作者:李诗怡 1. 金字塔原理 定义: 一种“先总后分、先结论后原因”的思考和表达方式。顶层为核心观点,中 ...
2026-09-22数据收集是数据分析、数据挖掘与数字化运营的源头工作,数据收集的完整性、准确性、时效性直接决定后续数据分析结果的可信度与业 ...
2026-09-21箱线图是数据分析中用于识别数据分布、判断离散程度、筛查异常值的核心图表。相比于直方图、趋势图,箱线图可以精准区分正常数据 ...
2026-09-21 很多数据分析师精通Excel函数和数据透视表,但当被问到“数据从哪里来”“表和视图有什么区别”“数据库管理系统和SQL是什么 ...
2026-09-21在数据库数据查询与数据分析工作中,日期时间是最常用的数据类型之一。原始数据库中存储的日期格式多为标准时间戳、年月日时分秒 ...
2026-09-20在时序数据分析中,增长率是衡量数据涨跌幅度、研判发展趋势、识别周期波动的核心指标,主要分为环比增长率与同比增长率。传统Ex ...
2026-09-20 很多企业团队并非缺乏指标,而是陷入“指标失控”:仪表盘上堆满实时跳动的数据,却无法回答“当前瓶颈在哪、下一步该做什么 ...
2026-09-20CDA数据分析师 出品 作者:李诗怡 STP模型(营销战略三步法) 定义: 现代营销战略核心框架,通过市场细分(S)、目标市场选择( ...
2026-09-18在互联网产品迭代、运营优化、界面改版与策略升级过程中,主观经验判断容易造成决策偏差、盲目改版、资源浪费等问题。AB实验(AB ...
2026-09-18 很多企业并不缺少指标,缺少的是让指标“串起来、动起来、用起来”的体系。零散指标像散落一地的珠子,指标体系则是那根把珠 ...
2026-09-18在电商行业精细化运营时代,流量红利逐步消退,粗放式投流、广撒网营销模式已无法适配市场竞争需求。依托用户点击、加购、收藏、 ...
2026-09-17在数据可视化与数据分析工作中,图表是将零散数据转化为直观业务规律的核心工具。不同图表拥有专属的数据逻辑与分析维度,能够从 ...
2026-09-17