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中死锁的形成示例及死锁情况的防止
Jun 14 Python
使用Python进行二进制文件读写的简单方法(推荐)
Sep 12 Python
Python Sqlite3以字典形式返回查询结果的实现方法
Oct 03 Python
Python 获取当前所在目录的方法详解
Aug 02 Python
Python 3中print函数的使用方法总结
Aug 08 Python
Pyqt实现无边框窗口拖动以及窗口大小改变
Apr 19 Python
pandas 读取各种格式文件的方法
Jun 22 Python
python使用udp实现聊天器功能
Dec 10 Python
python全栈要学什么 python全栈学习路线
Jun 28 Python
python pycharm的安装及其使用
Oct 11 Python
AUC计算方法与Python实现代码
Feb 28 Python
Django实现简单的分页功能
Feb 22 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
晶体管来复再生式二管收音机
2021/03/02 无线电
php使用多个进程同时控制文件读写示例
2014/02/28 PHP
zf框架的数据库追踪器使用示例
2014/03/13 PHP
利用PHP判断是否是连乘数字串的方法示例
2017/07/03 PHP
php实现根据身份证获取精准年龄
2020/02/26 PHP
简单的js分页脚本
2009/05/21 Javascript
JS URL传中文参数引发的乱码问题
2009/09/02 Javascript
JavaScript的变量作用域深入理解
2009/10/25 Javascript
ymPrompt的doHandler方法来实现获取子窗口返回值的方法
2010/06/25 Javascript
js判断样式className同时增加class或删除class
2013/01/30 Javascript
js数组操作学习总结
2013/11/04 Javascript
用Javascript获取页面元素的具体位置
2013/12/09 Javascript
jquery实现文本框textarea自适应高度
2016/03/09 Javascript
使用JQuery选择HTML遍历函数的方法
2016/09/17 Javascript
详解Vue使用 vue-cli 搭建项目
2017/04/20 Javascript
JS中cookie的使用及缺点讲解
2017/05/13 Javascript
用Vue-cli搭建的项目中引入css报错的原因分析
2017/07/20 Javascript
详解vue 计算属性与方法跟侦听器区别(面试考点)
2018/04/23 Javascript
vue-router实现编程式导航的代码实例
2019/01/19 Javascript
js form表单input框限制20个字符,10个汉字代码实例
2019/04/12 Javascript
ES6 Set结构的应用实例分析
2019/06/26 Javascript
Nuxt.js 静态资源和打包的操作
2020/11/06 Javascript
[02:09]EHOME夺得首届辉夜杯冠军—现场颁奖仪式
2015/12/28 DOTA
利用一个简单的例子窥探CPython内核的运行机制
2015/03/30 Python
Python爬取qq music中的音乐url及批量下载
2017/03/23 Python
python自动化脚本安装指定版本python环境详解
2017/09/14 Python
Python 实现淘宝秒杀的示例代码
2018/01/02 Python
解决TensorFlow模型恢复报错的问题
2020/02/06 Python
pytorch dataloader 取batch_size时候出现bug的解决方式
2020/02/20 Python
django之从html页面表单获取输入的数据实例
2020/03/16 Python
解决django migrate报错ORA-02000: missing ALWAYS keyword
2020/07/02 Python
KOHLER科勒美国官网:国际著名卫浴橱柜领先品牌
2020/06/27 全球购物
有关爱国演讲稿
2014/05/07 职场文书
社区法制宣传月活动总结
2015/05/07 职场文书
2016元旦文艺汇演主持词(开场白+结束语)
2015/12/03 职场文书
C站最全Python标准库总结,你想要的都在这里
2021/07/03 Python