python中利用队列asyncio.Queue进行通讯详解


Posted in Python onSeptember 10, 2017

前言

本文主要给大家介绍了关于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

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
Python简单生成随机姓名的方法示例
Dec 27 Python
基于Django URL传参 FORM表单传数据 get post的用法实例
May 28 Python
78行Python代码实现现微信撤回消息功能
Jul 26 Python
利用ctypes获取numpy数组的指针方法
Feb 12 Python
Python 中包/模块的 `import` 操作代码
Apr 22 Python
Python常用模块之requests模块用法分析
May 15 Python
在Pytorch中使用样本权重(sample_weight)的正确方法
Aug 17 Python
python matplotlib饼状图参数及用法解析
Nov 04 Python
python实现两个字典合并,两个list合并
Dec 02 Python
Macbook安装Python最新版本、GUI开发环境、图像处理、视频处理环境详解
Feb 17 Python
Python图像处理库PIL中图像格式转换的实现
Feb 26 Python
Python源码解析之List
May 21 Python
Python上下文管理器和with块详解
Sep 09 #Python
Python使用asyncio包处理并发详解
Sep 09 #Python
Python协程的用法和例子详解
Sep 09 #Python
python利用dir函数查看类中所有成员函数示例代码
Sep 08 #Python
Python使用回溯法子集树模板解决爬楼梯问题示例
Sep 08 #Python
Python使用回溯法子集树模板获取最长公共子序列(LCS)的方法
Sep 08 #Python
python中实现指定时间调用函数示例代码
Sep 08 #Python
You might like
php调整gif动画图片尺寸示例代码分享
2013/12/05 PHP
PHP针对字符串开头和结尾的判断方法
2016/07/11 PHP
PHP中十六进制颜色与RGB颜色值互转的方法
2019/03/18 PHP
js实现图片轮换效果代码
2013/04/16 Javascript
深入领悟JavaScript中的面向对象
2013/11/18 Javascript
jQuery中:reset选择器用法实例
2015/01/04 Javascript
javascript转换日期字符串为Date日期对象的方法
2015/02/13 Javascript
详解jQuery的表单验证插件--Validation
2016/12/21 Javascript
BootStrap Fileinput上传插件使用实例代码
2017/07/28 Javascript
微信小程序显示下拉列表功能【附源码下载】
2017/12/12 Javascript
解决vue项目nginx部署到非根目录下刷新空白的问题
2018/09/27 Javascript
微信小程序实现类似微信点击语音播放效果
2020/03/30 Javascript
基于layui的table插件进行复选框联动功能的实现方法
2019/09/19 Javascript
python返回昨天日期的方法
2015/05/13 Python
Python冒泡排序注意要点实例详解
2016/09/09 Python
Python守护进程和脚本单例运行详解
2017/01/06 Python
利用Tkinter和matplotlib两种方式画饼状图的实例
2017/11/06 Python
wxpython实现图书管理系统
2018/03/12 Python
使用python获取电脑的磁盘信息方法
2018/11/01 Python
Python学习笔记之pandas索引列、过滤、分组、求和功能示例
2019/06/03 Python
简单了解python高阶函数map/reduce
2019/06/28 Python
详解Python3迁移接口变化采坑记
2019/10/11 Python
Pytorch Tensor 输出为txt和mat格式方式
2020/01/03 Python
Python的控制结构之For、While、If循环问题
2020/06/30 Python
Django中和时区相关的安全问题详解
2020/10/12 Python
美国女士泳装店:Swimsuits For All
2017/03/02 全球购物
幼儿园教师工作制度
2014/01/22 职场文书
大学校园活动策划书
2014/02/04 职场文书
承诺书怎么写
2014/03/26 职场文书
一年级评语大全
2014/04/23 职场文书
导游词开场白
2015/01/31 职场文书
标枪加油稿
2015/07/22 职场文书
员工聘用合同范本
2015/09/21 职场文书
CentOS安装Nginx并部署vue
2022/04/12 Servers
Python中的 enumerate和zip详情
2022/05/30 Python
Linux在两个服务器直接传文件的操作方法
2022/08/05 Servers