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笔记(1) 关于我们应不应该继续学习python
Oct 24 Python
python数组过滤实现方法
Jul 27 Python
获取Django项目的全部url方法详解
Oct 26 Python
浅谈flask源码之请求过程
Jul 26 Python
利用Django模版生成树状结构实例代码
May 19 Python
Python实现字符串中某个字母的替代功能
Oct 21 Python
pandas读取csv文件提示不存在的解决方法及原因分析
Apr 21 Python
基于Python把网站域名解析成ip地址
May 25 Python
pandas创建DataFrame的7种方法小结
Jun 14 Python
Python代码注释规范代码实例解析
Aug 14 Python
Python中三维坐标空间绘制的实现
Sep 22 Python
pytorch 中autograd.grad()函数的用法说明
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下安装配置fckeditor编辑器的方法
2011/03/02 PHP
在PHP中设置、使用、删除Cookie的解决方法
2013/05/06 PHP
php 检查电子邮件函数(自写)
2014/01/16 PHP
php+ajax实现文章自动保存的方法
2014/12/30 PHP
windows平台中配置nginx+php环境
2015/12/06 PHP
PHP数字前补0的自带函数sprintf 和number_format的用法(详解)
2017/02/06 PHP
在php7中MongoDB实现模糊查询的方法详解
2017/05/03 PHP
Javascript异步表单提交,图片上传,兼容异步模拟ajax技术
2010/05/10 Javascript
JavaScript 的继承
2011/10/01 Javascript
利用JS延迟加载百度分享代码,提高网页速度
2013/07/01 Javascript
jquery实现微博文字输入框 输入时显示输入字数 效果实现
2013/07/12 Javascript
对Web开发中前端框架与前端类库的一些思考
2015/03/27 Javascript
EasyUI 中combotree 默认不能选择父节点的实现方法
2016/11/07 Javascript
一个炫酷的Bootstrap导航菜单
2016/12/28 Javascript
vue实现移动端图片裁剪上传功能
2020/08/18 Javascript
使用JS和canvas实现gif动图的停止和播放代码
2017/09/01 Javascript
细说webpack源码之compile流程-rules参数处理技巧(1)
2017/12/26 Javascript
vue实现在表格里,取每行的id的方法
2018/03/09 Javascript
Vue中 v-if/v-show/插值表达式导致闪现的原因及解决办法
2018/10/12 Javascript
微信小程序实现的动态设置导航栏标题功能示例
2019/01/31 Javascript
ES6实现图片切换特效代码
2020/01/14 Javascript
vue 项目软键盘回车触发搜索事件
2020/09/09 Javascript
Python3 入门教程 简单但比较不错
2009/11/29 Python
Python设计模式之备忘录模式原理与用法详解
2019/01/15 Python
python2 对excel表格操作完整示例
2020/02/23 Python
python 数据分析实现长宽格式的转换
2020/05/18 Python
数据管理员的自我评价分享
2013/11/15 职场文书
医学专业职业生涯规划范文
2014/02/05 职场文书
培训讲师岗位职责
2014/04/13 职场文书
个人评语大全
2014/05/04 职场文书
员工三分钟演讲稿
2014/08/19 职场文书
敬老院义诊活动总结
2015/05/07 职场文书
员工聘用合同范本
2015/09/21 职场文书
position:sticky 粘性定位的几种巧妙应用详解
2021/04/24 HTML / CSS
React Fragment介绍与使用详解
2021/11/11 Javascript
聊聊SpringBoot自动装配的魔力
2021/11/17 Java/Android