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编写的最短路径算法
Mar 25 Python
在Python中处理字符串之isdigit()方法的使用
May 18 Python
Python使用os模块和fileinput模块来操作文件目录
Jan 19 Python
小议Python中自定义函数的可变参数的使用及注意点
Jun 21 Python
Python读取文件内容的三种常用方式及效率比较
Oct 07 Python
Django Rest framework之认证的实现代码
Dec 17 Python
在Python中通过getattr获取对象引用的方法
Jan 21 Python
python中while和for的区别总结
Jun 28 Python
python Tensor和Array对比分析
Jan 08 Python
python 图像插值 最近邻、双线性、双三次实例
Jul 05 Python
详解Pycharm安装及Django安装配置指南
Sep 15 Python
Python numpy大矩阵运算内存不足如何解决
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
在php中判断一个请求是ajax请求还是普通请求的方法
2011/06/28 PHP
PHP设置图片文件上传大小的具体实现方法
2013/10/11 PHP
thinkphp浏览历史功能实现方法
2014/10/29 PHP
php进程间通讯实例分析
2016/07/11 PHP
jquery实现的带缩略图的焦点图片切换(自动播放/响应鼠标动作)
2013/01/23 Javascript
JS中数组Array的用法示例介绍
2014/02/20 Javascript
JavaScript中合并数组的N种方法
2014/09/16 Javascript
JS简单实现点击复制链接的方法
2016/08/03 Javascript
火狐和ie下获取javascript 获取event的方法(推荐)
2016/11/26 Javascript
js实现开启密码大写提示
2016/12/21 Javascript
基于jquery trigger函数无法触发a标签的两种解决方法
2018/01/06 jQuery
如何封装了一个vue移动端下拉加载下一页数据的组件
2019/01/06 Javascript
浅谈vue中document.getElementById()拿到的是原值的问题
2020/07/26 Javascript
原生js 实现表单验证功能
2021/02/08 Javascript
python pickle 和 shelve模块的用法
2013/09/16 Python
剖析Python的Twisted框架的核心特性
2016/05/25 Python
Python设计模式之门面模式简单示例
2018/01/09 Python
解决phantomjs截图失败,phantom.exit位置的问题
2018/05/17 Python
Python读取txt某几列绘图的方法
2018/10/14 Python
jenkins配置python脚本定时任务过程图解
2019/10/29 Python
Python 抓取数据存储到Redis中的操作
2020/07/16 Python
python zip()函数的使用示例
2020/09/23 Python
Flask中jinja2的继承实现方法及实例
2021/03/03 Python
localStorage的过期时间设置的方法详解
2018/11/26 HTML / CSS
澳大利亚宠物商店:Petbarn
2017/11/18 全球购物
全球地下的服装和态度:Slam Jam
2018/02/04 全球购物
俄罗斯皮肤健康中心:Pharmacosmetica.ru
2020/02/22 全球购物
Java语言程序设计测试题判断题部分
2013/01/06 面试题
会计学个人自荐信模板
2013/12/13 职场文书
2014年元旦联欢会活动策划方案
2014/02/16 职场文书
县政府办公室领导班子对照检查材料思想汇报
2014/09/28 职场文书
六查六看自检自查剖析材料
2014/10/14 职场文书
硕士学位申请报告
2015/05/15 职场文书
2019个人年度目标制定攻略!
2019/07/12 职场文书
Redis分布式锁的7种实现
2022/04/01 Redis
WINDOWS下安装mysql 8.x 的方法图文教程
2022/04/19 MySQL