python中利用队列asyncio.Queue进行通讯详解
python中利用队列asyncio.Queue进行通讯详解
本文主要给大家介绍了关于python用队列asyncio.Queue通讯的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍吧。
asyncio.Queue与其它队列是一样的,都是先进先出,它是为协程定义的
例子如下:
import asyncio
async def consumer(n, q):
async def producer(q, num_workers):
async def main(loop, num_consumers):
event_loop = asyncio.get_event_loop()
try:
finally:
输出如下:
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+OpenCV实现AI人脸识别身份认证系统(1)—人脸识别原理
- python登录豆瓣,发帖
- Python 字符串_python 字符串截取_python 字符串替换_python 字符串连接
- python 检测字符串编码类型是什么
- python 关于celery的异步任务队列的基本使用(celery+redis)【采用配置文件设置】
- Python:python语言中与时间有关的库函数简介、安装、使用方法(获取当前时间/计算程序块前后运行时间/模型训练时间或耗费时间)之详细攻略
- Python语言学习之字符串那些事:python和字符串的使用方法之详细攻略
- Python语言学习之常见语句命令那些事:python和常见语句命令(条件语句、pass语句)使用方法之详细攻略
- Python之ffmpeg-python:ffmpeg-python库的简介、安装、使用方法之详细攻略
- 已解决Visit python.org to download an earlier version of Python.
- 【python】昔去雪如花,今来花似雪,今日份雪花快递到啦
- 可视化 Python 打包 exe,这个神器绝了!
- Python编程:queue队列
- Python编程:RabbitMQ消息队列
- Python编程:命令行python和python -m的区别
- Python编程:cv2模块对图像处理的基本操作
- Python标准库:内置函数isinstance(object, classinfo)
- selenium多窗口、句柄!什么?你不明白这些操作?(python篇)
- Python+Selenium+Threading进行兼容性测试
- Python 多进程异常处理
- Python常用内置函数(python 3.x)