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 cs架构实现简单文件传输
Mar 20 Python
django框架自定义用户表操作示例
Aug 07 Python
python自动保存百度盘资源到百度盘中的实例代码
Aug 26 Python
python实现KNN分类算法
Oct 16 Python
python基于celery实现异步任务周期任务定时任务
Dec 30 Python
Pytorch 数据加载与数据预处理方式
Dec 31 Python
解决Tensorboard可视化错误:不显示数据 No scalar data was found
Feb 15 Python
python批量修改xml属性的实现方式
Mar 05 Python
动态设置django的model field的默认值操作步骤
Mar 30 Python
Python字典fromkeys()方法使用代码实例
Jul 20 Python
python打包生成so文件的实现
Oct 30 Python
教你使用Pandas直接核算Excel中快递费用
May 12 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图片加水印原理(超简单的实例代码)
2013/01/18 PHP
谈谈关于php的优点与缺点
2013/04/11 PHP
php自动给网址加上链接的方法
2015/06/02 PHP
joomla实现注册用户添加新字段的方法
2016/05/05 PHP
PHP实现的简单分页类及用法示例
2016/05/06 PHP
Jquery 数据选择插件Pickerbox使用介绍
2012/08/24 Javascript
js获取上传文件大小示例代码
2014/04/10 Javascript
js使用正则实现ReplaceAll全部替换的方法
2014/07/18 Javascript
jQuery元素选择器用法实例
2014/12/23 Javascript
javascript实现3D变换的立体圆圈实例
2015/08/06 Javascript
javascript日期操作详解(脚本之家整理)
2015/09/05 Javascript
javascript中apply、call和bind的使用区别
2016/04/05 Javascript
JS实现title标题栏文字不间断滚动显示效果
2016/09/07 Javascript
详解在vue-cli中引用jQuery、bootstrap以及使用sass、less编写css
2017/11/08 jQuery
新手快速入门JavaScript装饰者模式与AOP
2019/06/24 Javascript
vue中js判断长时间不操作界面自动退出登录(推荐)
2020/01/22 Javascript
2款Python内存检测工具介绍和使用方法
2014/06/01 Python
Django重装mysql后启动报错:No module named ‘MySQLdb’的解决方法
2018/04/22 Python
解决python opencv无法显示图片的问题
2018/10/28 Python
PIL对上传到Django的图片进行处理并保存的实例
2019/08/07 Python
对python中UDP,socket的使用详解
2019/08/22 Python
opencv之颜色过滤只留下图片中的红色区域操作
2020/06/05 Python
Python 整行读取文本方法并去掉readlines换行\n操作
2020/09/03 Python
python3从网络摄像机解析mjpeg http流的示例
2020/11/13 Python
英国假睫毛购买网站:FalseEyelashes.co.uk
2018/05/23 全球购物
巴西服装和鞋子购物网站:Marisa
2018/10/25 全球购物
CK加拿大官网:Calvin Klein加拿大
2020/03/14 全球购物
2019年c语言经典面试题目
2016/08/17 面试题
什么是索引指示器
2012/08/20 面试题
研究生自荐信
2013/10/09 职场文书
企业业务员岗位职责
2014/03/14 职场文书
个人四风问题整改措施思想汇报
2014/10/04 职场文书
2015年初中生自我评价范文
2015/03/03 职场文书
学校教学管理制度
2015/08/06 职场文书
浅谈Redis跟MySQL的双写问题解决方案
2022/02/24 Redis
Python OpenCV超详细讲解调整大小与图像操作的实现
2022/04/02 Python