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模块学习 re 正则表达式
May 19 Python
python监控网站运行异常并发送邮件的方法
Mar 13 Python
将Python中的数据存储到系统本地的简单方法
Apr 11 Python
python3.5 tkinter实现页面跳转
Jan 30 Python
python中数据爬虫requests库使用方法详解
Feb 11 Python
python装饰器-限制函数调用次数的方法(10s调用一次)
Apr 21 Python
python list转矩阵的实例讲解
Aug 04 Python
pycharm使用matplotlib.pyplot不显示图形的解决方法
Oct 28 Python
解决Python中回文数和质数的问题
Nov 24 Python
离线状态下在jupyter notebook中使用plotly实例
Apr 24 Python
Python排序函数的使用方法详解
Dec 11 Python
用Python将GIF动图分解成多张静态图片
Jun 11 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新手上路(六)
2006/10/09 PHP
浅析PHP中的字符串编码转换(自动识别原编码)
2013/07/02 PHP
实现获取http内容的php函数分享
2014/02/16 PHP
cookie 最近浏览记录(中文escape转码)具体实现
2013/06/08 Javascript
jQuery中:selected选择器用法实例
2015/01/04 Javascript
js css自定义分页效果
2017/02/24 Javascript
discuz表情的JS提取方法分析
2017/03/22 Javascript
AngularJS之自定义服务详解(factory、service、provider)
2017/04/14 Javascript
微信小程序checkbox组件使用详解
2018/01/31 Javascript
Vue render深入开发讲解
2018/04/13 Javascript
6行代码实现微信小程序页面返回顶部效果
2018/12/28 Javascript
fastadmin中调用js的方法
2019/05/14 Javascript
Vue+Node服务器查询Mongo数据库及页面数据传递操作实例分析
2019/12/20 Javascript
Python中的模块和包概念介绍
2015/04/13 Python
Python实现拷贝多个文件到同一目录的方法
2016/09/19 Python
Python中创建二维数组
2018/10/17 Python
django框架model orM使用字典作为参数,保存数据的方法分析
2019/06/24 Python
python调用并链接MATLAB脚本详解
2019/07/05 Python
使用python实现滑动验证码功能
2019/08/05 Python
Python 实现打印单词的菱形字符图案
2020/04/12 Python
Python内置函数及功能简介汇总
2020/10/13 Python
python+requests实现接口测试的完整步骤
2020/10/27 Python
CSS3 box-sizing属性详解
2016/11/15 HTML / CSS
html5本地存储之localstorage 、本地数据库、sessionStorage简单使用示例
2014/05/08 HTML / CSS
法国综合购物网站:RueDuCommerce
2016/09/12 全球购物
双立人加拿大官网:Zwilling加拿大
2020/08/10 全球购物
师范毕业生求职自荐信
2013/09/25 职场文书
八年级音乐教学反思
2014/01/09 职场文书
探矿工程师自荐信
2014/01/24 职场文书
2014学雷锋活动心得体会
2014/03/10 职场文书
四川省传达学习贯彻党的群众路线教育实践活动总结大会精神新闻稿
2014/10/26 职场文书
普宁寺导游词
2015/02/04 职场文书
2015年办公室个人工作总结
2015/04/20 职场文书
CSS3鼠标悬浮过渡缩放效果
2021/04/17 HTML / CSS
浅谈sql_@SelectProvider及使用注意说明
2021/08/04 Java/Android
Python如何用re模块实现简易tokenizer
2022/05/02 Python