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使用分治法实现求解最大值的方法
May 12 Python
python简单文本处理的方法
Jul 10 Python
python实现求最长回文子串长度
Jan 22 Python
10 行Python 代码实现 AI 目标检测技术【推荐】
Jun 14 Python
python解析xml简单示例
Jun 21 Python
如何实现Django Rest framework版本控制
Jul 25 Python
python中从for循环延申到推导式的具体使用
Nov 29 Python
python scrapy重复执行实现代码详解
Dec 28 Python
Python实现队列的方法示例小结【数组,链表】
Feb 22 Python
python GUI库图形界面开发之PyQt5动态(可拖动控件大小)布局控件QSplitter详细使用方法与实例
Mar 06 Python
Python基于codecs模块实现文件读写案例解析
May 11 Python
Python中return函数返回值实例用法
Nov 19 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
PHP5 安装方法
2007/01/15 PHP
一周让你学会PHP 不错的学习资料
2009/02/06 PHP
46 个非常有用的 PHP 代码片段
2016/02/16 PHP
php设计模式之观察者模式定义与用法经典示例
2019/09/19 PHP
使用Modello编写JavaScript类
2006/12/22 Javascript
使用户点击后退按钮使效三行代码
2007/07/07 Javascript
javascript 面向对象继承
2009/11/26 Javascript
StringTemplate遇见jQuery冲突的解决方法
2011/09/22 Javascript
利用javascript实现全部删或清空所选的操作
2014/05/27 Javascript
轻松创建nodejs服务器(3):代码模块化
2014/12/18 NodeJs
JavaScript中的变量作用域介绍
2014/12/31 Javascript
jquery获取文档高度和窗口高度汇总
2016/01/25 Javascript
Bootstrap3 input输入框插入glyphicon图标的方法
2016/05/16 Javascript
JS留言功能的简单实现案例(推荐)
2016/06/23 Javascript
vue2实现移动端上传、预览、压缩图片解决拍照旋转问题
2017/04/13 Javascript
javascript性能优化之分时函数的介绍
2018/03/28 Javascript
基于node搭建服务器,写接口,调接口,跨域的实例
2018/05/13 Javascript
基于Ionic3实现选项卡切换并重新加载echarts
2020/09/24 Javascript
[07:06]2018DOTA2国际邀请赛寻真——卫冕冠军Team Liquid
2018/08/10 DOTA
在Python中利用Pandas库处理大数据的简单介绍
2015/04/07 Python
python实现清屏的方法
2015/04/30 Python
Python读取文件内容的三种常用方式及效率比较
2017/10/07 Python
Python SSL证书验证问题解决方案
2020/01/13 Python
python GUI库图形界面开发之PyQt5 Qt Designer工具(Qt设计师)详细使用方法及Designer ui文件转py文件方法
2020/02/26 Python
Python任务自动化工具tox使用教程
2020/03/17 Python
Python通过kerberos安全认证操作kafka方式
2020/06/06 Python
Python collections.deque双边队列原理详解
2020/10/05 Python
CSS3实现的闪烁跳跃进度条示例(附源码)
2013/08/19 HTML / CSS
安踏官方商城:anta.cn
2019/12/16 全球购物
考试不及格的检讨书
2014/01/22 职场文书
2014年文学毕业生自我鉴定
2014/04/23 职场文书
带病坚持工作事迹
2014/05/03 职场文书
林肯就职演讲稿
2014/05/19 职场文书
询价采购方案
2014/06/09 职场文书
向国旗敬礼活动总结
2014/09/27 职场文书
党的群众路线教育实践活动批评与自我批评发言稿
2014/10/16 职场文书