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面试基础知识_python自学需要哪些基础知识
- aic准则python_Python数据科学:线性回归
- python人工智能学习笔记_[Python] 人工智能与自然语言处理学习笔记(1)[通俗易懂]
- Python入门系列(六)一篇学会python函数
- python中sqrt函数用法_Python : sqrt() 函数
- gyp ERR! stack Error: Can't find Python executable 'python'
- python pkl文件_Python字符串格式化输出的方式包括
- python chmod_Python os.chmod用法及代码示例
- python hexdump_笨办法学 Python · 续 练习 26:`hexdump`
- 学习Python与Excel:使用xlwt在没有Excel的情况下编写电子表格
- Python标识符的命名规则有哪些?Python标识符命名规范及常见问题
- 使用Python发送邮件
- Python基础语法-基本数据类型-整数
- python-Python与SQLite数据库-SQLite数据库的基本知识(一)
- python-Python与SQLite数据库-使用Python执行SQLite查询(一)
- python-Python与MongoDB数据库-使用Python执行MongoDB查询(二)
- python批量抓取美女图片详解编程语言
- Hello World之Python版详解编程语言
- 用Python简化Linux指令:从繁琐变简单(python运行linux命令)
- 利用Neo4j和Python进行无缝图形数据库支持(neo4j python)
- Python如何使用MySQL构建立连接(python怎么连接mysql)
- Linux Python:开启互联网新时代(linux python)
- Python中使用MySQL数据库的操作技巧(mysql_python)
- Python文件夹与文件的操作实现代码
- 初学Python实用技巧两则
- Python深入学习之装饰器