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 pass 语句使用示例
Mar 11 Python
python中MySQLdb模块用法实例
Nov 10 Python
Python生成随机密码
Mar 10 Python
进一步理解Python中的函数编程
Apr 13 Python
Python 模拟员工信息数据库操作的实例
Oct 23 Python
python 将列表中的字符串连接成一个长路径的方法
Oct 23 Python
Python pycharm 同时加载多个项目的方法
Jan 17 Python
详解python读取和输出到txt
Mar 29 Python
ubuntu 16.04下python版本切换的方法
Jun 14 Python
Python argparse模块应用实例解析
Nov 15 Python
Jupyter notebook无法导入第三方模块的解决方式
Apr 15 Python
keras分类之二分类实例(Cat and dog)
Jul 09 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
再说下636单管机
2021/03/02 无线电
简单的用PHP编写的导航条程序
2006/10/09 PHP
PHP提取数据库内容中的图片地址并循环输出
2010/03/21 PHP
php中防止伪造跨站请求的小招式
2011/09/02 PHP
PHP中对数组的一些常用的增、删、插操作函数总结
2015/11/27 PHP
javascript 设置文本框中焦点的位置
2009/11/20 Javascript
屏蔽F1~F12的快捷键的js函数
2010/05/06 Javascript
IE本地存储userdata的一个bug说明
2010/07/01 Javascript
jsTree 基于JQuery的排序节点 Bug
2011/07/26 Javascript
javascript数组去重的方法汇总
2015/04/14 Javascript
js简单判断移动端系统的方法
2016/02/25 Javascript
JS留言功能的简单实现案例(推荐)
2016/06/23 Javascript
微信小程序 缓存(本地缓存、异步缓存、同步缓存)详解
2017/01/17 Javascript
nodejs入门教程五:连接数据库的方法分析
2017/04/24 NodeJs
360doc网站不登录就无法复制内容的解决方法
2018/01/27 Javascript
快速解决Vue项目在IE浏览器中显示空白的问题
2018/09/04 Javascript
Node.js JSON模块用法实例分析
2019/01/04 Javascript
微信小程序实现获取用户信息并存入数据库操作示例
2019/05/07 Javascript
Vue请求java服务端并返回数据代码实例
2019/11/28 Javascript
RxJS在TypeScript中的简单使用详解
2020/04/13 Javascript
解决在Vue中使用axios POST请求变成OPTIONS的问题
2020/08/14 Javascript
python中的闭包用法实例详解
2015/05/05 Python
Python 文件处理注意事项总结
2017/04/10 Python
简单的python协同过滤程序实例代码
2018/01/31 Python
python3实现随机数
2018/06/25 Python
Flask教程之重定向与错误处理实例分析
2019/08/01 Python
美国最大的团购网站:Groupon
2016/07/23 全球购物
澳大利亚票务和娱乐市场领导者:Ticketmaster
2017/03/03 全球购物
.net面试题
2016/09/17 面试题
Java面试题汇总
2015/12/06 面试题
工商管理自荐书
2014/07/06 职场文书
2015年度优秀员工推荐信
2015/03/23 职场文书
贫困证明书范文
2015/06/16 职场文书
优秀教师工作总结2015
2015/07/22 职场文书
Nginx配置并兼容HTTP实现代码解析
2021/03/31 Servers
CentOS7环境下MySQL8常用命令小结
2022/06/10 Servers