python里使用Condition对象来唤醒指定数量的协程
2023-09-14 09:10:35 时间
在asyncio库里,定义Condition对象,它的行为与事件Event有点像,区别是事件是通知所有对象,Condition对象可以指定一定数量的协程被通知,它是通过函数notify()来实现的,如果参数里放2,就是通知两个协程,例子如下:
import asyncio
async def consumer(condition, n):
with await condition:
print('consumer {} is waiting'.format(n))
await condition.wait()
print('consumer {} triggered'.format(n))
print('ending consumer {}'.format(n))
async def manipulate_condition(condition):
print('starting manipulate_condition')
# pause to let consumers start
await asyncio.sleep(0.1)
for i in range(1, 3):
with await condition:
print('notifying {} consumers'.format(i))
condition.notify(n=i)
await asyncio.sleep(0.1)
with await condition:
print('notifying remaining consumers')
condition.notify_all()
print('ending manipulate_condition')
async def main(loop):
# Create a condition
condition = asyncio.Condition()
# Set up tasks watching the condition
consumers = [
consumer(condition, i)
for i in range(5)
]
# Schedule a task to manipulate the condition variable
loop.create_task(manipulate_condition(condition))
# Wait for the consumers to be done
await asyncio.wait(consumers)
event_loop = asyncio.get_event_loop()
try:
result = event_loop.run_until_complete(main(event_loop))
finally:
event_loop.close()
结果输出如下:
starting manipulate_condition
consumer 2 is waiting
consumer 3 is waiting
consumer 4 is waiting
consumer 1 is waiting
consumer 0 is waiting
notifying 1 consumers
consumer 2 triggered
ending consumer 2
notifying 2 consumers
consumer 3 triggered
ending consumer 3
consumer 4 triggered
ending consumer 4
notifying remaining consumers
ending manipulate_condition
consumer 1 triggered
ending consumer 1
consumer 0 triggered
ending consumer 0
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脚本把sqlAlchemy对象转换成dict的教程
- Python - 使用objgraph生成对象引用关系图
- Python 如何判断一个对象是否为单链表的节点
- python django admin.site.register注册应用
- paip. 调试技术打印堆栈 uapi print stack java php python 总结.
- Py之jieba:Python包之jieba包(中文分词最好的组件)简介、安装、使用方法之详细攻略
- Python编程语言学习:python语言中快速查询python自带模块&函数的用法及其属性方法、如何查询某个函数&关键词的用法、输出一个类或者实例化对象的所有属性和方法名之详细攻略
- 十个小项目带你学会python编程,很简单,识字就行
- 〖Python 数据库开发实战 - MongoDB篇⑦〗- 开启 MongoDB 的登录验证功能
- 【阶段四】Python深度学习04篇:深度学习项目实战:深度神经网络预测客户流失率(分类模型)
- Python学习78:XML
- Python学习40:访问限制
- 【华为机试真题 Python实现】竖直四子棋
- 【Python实验】选择和循环结构
- python之制作MP3文件
- anndata scanpy拆分成多个数据集python Linux 多个单细胞合并 切分数据集合 切割
- Python解释器的探讨:第一部分函数对象
- python里使用await关键字来等待Future对象完成
- python基础===isinstance() 函数,判断一个对象是否是一个已知的类型
- python实战===用python对比两张图片的不同
- python基于协程的网络库gevent、eventlet
- Python实现将图片上传到基于阿里云OSS对象存储的图床
- 转载:大厂5G python自动化测试面试必会 | 对象与面向对象(1)
- 第11讲:Python列表对象中元素修改操作
- Python:统计大小写字母个数和数字个数