python里使用队列asyncio.Queue进行通讯
来源:互联网 发布:苹果软件商店打不开 编辑:程序博客网 时间:2024/06/18 18:47
asyncio.Queue与其它队列是一样的,都是先进先出,它是为协程定义的,例子如下:
import asyncioasync 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游戏开发入门
你也能动手修改C编译器
纸牌游戏开发
http://edu.csdn.net/course/detail/5538
五子棋游戏开发
http://edu.csdn.net/course/detail/5487
RPG游戏从入门到精通
http://edu.csdn.net/course/detail/5246
WiX安装工具的使用
http://edu.csdn.net/course/detail/5207
俄罗斯方块游戏开发
http://edu.csdn.net/course/detail/5110
boost库入门基础
http://edu.csdn.net/course/detail/5029
Arduino入门基础
http://edu.csdn.net/course/detail/4931
Unity5.x游戏基础入门
http://edu.csdn.net/course/detail/4810
TensorFlow API攻略
http://edu.csdn.net/course/detail/4495
TensorFlow入门基本教程
http://edu.csdn.net/course/detail/4369
C++标准模板库从入门到精通
http://edu.csdn.net/course/detail/3324
跟老菜鸟学C++
http://edu.csdn.net/course/detail/2901
跟老菜鸟学python
http://edu.csdn.net/course/detail/2592
在VC2015里学会使用tinyxml库
http://edu.csdn.net/course/detail/2590
在Windows下SVN的版本管理与实战
http://edu.csdn.net/course/detail/2579
Visual Studio 2015开发C++程序的基本使用
http://edu.csdn.net/course/detail/2570
在VC2015里使用protobuf协议
http://edu.csdn.net/course/detail/2582
在VC2015里学会使用MySQL数据库
http://edu.csdn.net/course/detail/2672
- python里使用队列asyncio.Queue进行通讯
- Python asyncio使用范例
- python:利用asyncio进行快速抓取
- python:利用asyncio进行快速抓取
- python里使用事件对象asyncio.Event来同步协程
- 使用 aiohttp 和 asyncio 进行异步请求
- python使用queue队列实现生产者消费者
- 在Windows里生成SSL的证书和使用python进行通讯
- python asyncio
- Python 使用 asyncio 包处理并发
- python Queue队列例子
- Python队列Queue模块
- python队列Queue
- Python队列Queue
- python队列Queue
- python队列Queue
- Unity3d shader中Queue 使用渲染队列进行深度排序
- JAVA QUEUE 队列 使用
- vSphere笔记01~02
- HTTP协议
- 华硕笔记本快速启动项里面没有usb设备解决办法
- 顺序线性表 代码实现(基于java)
- js 事件代理(事件委托)
- python里使用队列asyncio.Queue进行通讯
- Tabel控件 4.3.10
- 小学期 比赛计分问题
- JS每周读书笔记[3]--Date & RegExp
- 麻省理工学院-2017年-深度学习与自动驾驶视频课程分享
- Android Studio发布项目到jcenter,一行代码引入Module
- js获取网页高度
- cocos2d-x 3.0的lua-binding接口导出
- 简谈高通Trustzone的实现