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实现的数据结构与算法之快速排序详解
Apr 22 Python
Python中的深拷贝和浅拷贝详解
Jun 03 Python
Python数据可视化编程通过Matplotlib创建散点图代码示例
Dec 09 Python
python中实现数组和列表读取一列的方法
Apr 03 Python
Django Admin实现三级联动的示例代码(省市区)
Jun 22 Python
Python3 获取一大段文本之间两个关键字之间的内容方法
Oct 11 Python
浅析Python 3 字符串中的 STR 和 Bytes 有什么区别
Oct 14 Python
python操作文件的参数整理
Jun 11 Python
使用Pyinstaller转换.py文件为.exe可执行程序过程详解
Aug 06 Python
使用OpenCV实现仿射变换—平移功能
Aug 29 Python
Win10下python 2.7与python 3.7双环境安装教程图解
Oct 12 Python
python文件名批量重命名脚本实例代码
Apr 22 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
Warning: session_destroy() : Trying to destroy uninitialized sessionq错误
2011/06/16 PHP
PHP小教程之实现链表
2014/06/09 PHP
php实现mysql事务处理的方法
2014/12/25 PHP
php邮件发送的两种方式
2020/04/28 PHP
ThinkPHP下表单令牌错误与解决方法分析
2017/05/20 PHP
Extjs4 消息框去掉关闭按钮(类似Ext.Msg.alert)
2013/04/02 Javascript
限制textbox或textarea输入字符长度的JS代码
2013/10/16 Javascript
Extjs 4.x 得到form CheckBox 复选框的值
2014/05/04 Javascript
jquery访问ashx文件示例代码
2014/08/11 Javascript
轻量级javascript 框架Backbone使用指南
2015/07/24 Javascript
jQuery基于ajax实现带动画效果无刷新柱状图投票代码
2015/08/10 Javascript
Angular在一个页面中使用两个ng-app的方法(二)
2017/02/20 Javascript
原生js实现吸顶效果
2017/03/13 Javascript
vue动态生成dom并且自动绑定事件
2017/04/19 Javascript
angularjs $http实现form表单提交示例
2017/06/09 Javascript
angularjs中判断ng-repeat是否迭代完的实例
2018/09/12 Javascript
Vue数据驱动表单渲染,轻松搞定form表单
2019/07/19 Javascript
关于ES6尾调用优化的使用
2020/09/11 Javascript
Python内置的HTTP协议服务器SimpleHTTPServer使用指南
2016/03/30 Python
python方向键控制上下左右代码
2018/01/20 Python
在pandas多重索引multiIndex中选定指定索引的行方法
2018/11/16 Python
python 自定义对象的打印方法
2019/01/12 Python
Python实现最常见加密方式详解
2019/07/13 Python
python tkinter基本属性详解
2019/09/16 Python
Python对Excel按列值筛选并拆分表格到多个文件的代码
2019/11/05 Python
Python如何操作office实现自动化及win32com.client的运用
2020/04/01 Python
基于Python编写一个计算器程序,实现简单的加减乘除和取余二元运算
2020/08/05 Python
全面总结使用CSS实现水平垂直居中效果的方法
2016/03/10 HTML / CSS
css3使网页、图片变成灰色兼容大多数浏览器
2014/07/02 HTML / CSS
使用PDF.JS插件在HTML中预览PDF文件的方法
2018/08/29 HTML / CSS
美国第一香水网站:Perfume.com
2017/01/23 全球购物
俄罗斯香水在线商店:AromaCode
2019/12/04 全球购物
法律专业实习鉴定
2013/12/22 职场文书
旷课检讨书1000字
2014/02/14 职场文书
三年级小学生评语
2014/04/22 职场文书
应聘教师求职信范文
2015/03/20 职场文书