python多线程高级锁condition简单用法示例
作者:weixin_42359464 时间:2023-10-31 16:11:23
本文实例讲述了python多线程高级锁condition简单用法。分享给大家供大家参考,具体如下:
多线程编程中如果使用Condition对象代替lock, 能够实现在某个事件触发后才处理数据, condition中含有的方法:
- wait:线程挂起,收到notify通知后继续运行
- notify:通知其他线程, 解除其它线程的wai状态
- notifyAll(): 通知所有线程
- acquire和release: 获得锁和解除锁, 与lock类似,
- enter和exit使得对象支持上下文操作:
def __enter__(self):
return self._lock.__enter__()
def __exit__(self, *args):
return self._lock.__exit__(*args)
代码:
import threading
from threading import Condition
# condition
class XiaoAi(threading.Thread):
def __init__(self, cond):
self.cond = cond
super().__init__(name="xiaoai")
def run(self):
self.cond.acquire()
self.cond.wait()
print('{}:ennn. '.format(self.name))
self.cond.notify()
self.cond.wait()
print('{}:好嗒. '.format(self.name))
self.cond.release()
class TianMao(threading.Thread):
def __init__(self, cond):
super().__init__(name="tiaomao")
self.cond = cond
def run(self):
self.cond.acquire()
print('{}:hello ~ xiaoai. '.format(self.name))
self.cond.notify()
self.cond.wait()
print('{}:我们来念一首诗吧! . '.format(self.name))
self.cond.notify()
self.cond.release()
if __name__ == '__main__':
condition = Condition()
xiaoai = XiaoAi(condition)
tianmao = TianMao(condition)
# 启动顺序很重要
xiaoai.start()
tianmao.start()
打印结果:
tiaomao:hello ~ xiaoai.
xiaoai:ennn.
tiaomao:我们来念一首诗吧! .
xiaoai:好嗒
总结:
这个比较鸡肋
希望本文所述对大家Python程序设计有所帮助。
来源:https://blog.csdn.net/weixin_42359464/article/details/81007495
标签:python,多线程,高级锁,condition
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
云原生使用Docker部署mysql数据库的详细过程
2024-01-20 15:57:24
MySQL Server层四个日志的实现
2024-01-21 11:16:58
![](https://img.aspxhome.com/file/2023/8/117838_0s.jpg)
Python之Web框架Django项目搭建全过程
2022-03-21 03:06:08
![](https://img.aspxhome.com/file/2023/3/93853_0s.png)
写给JavaScript库开发者们的规则
2008-10-26 12:30:00
MYSQL教程:服务器优化和硬件优化
2009-02-27 15:43:00
简单谈谈Python中的模块导入
2021-02-24 20:28:17
Pandas时间序列重采样(resample)方法中closed、label的作用详解
2023-01-31 17:46:36
django rest framework vue 实现用户登录详解
2022-04-15 00:53:52
![](https://img.aspxhome.com/file/2023/1/101171_0s.png)
PyCharm+PyQt5+QtDesigner配置详解
2023-09-11 12:40:03
![](https://img.aspxhome.com/file/2023/0/131130_0s.jpg)
浅析设计与内容呈现的关系
2009-08-06 18:12:00
![](https://img.aspxhome.com/file/UploadPic/20098/6/011-700x530-51s.png)
Python图像运算之图像点运算与灰度化处理详解
2021-06-15 23:43:14
![](https://img.aspxhome.com/file/2023/8/115158_0s.png)
OpenCV 图像分割实现Kmean聚类的示例代码
2023-08-10 18:59:41
![](https://img.aspxhome.com/file/2023/3/64703_0s.jpg)
SQL Server 2000安装图解教程
2009-09-09 19:59:00
![](https://img.aspxhome.com/file/UploadPic/up/2009090920263675.gif)
python实现windows下文件备份脚本
2021-05-06 06:32:41
特征脸(Eigenface)理论基础之PCA主成分分析法
2023-03-03 07:19:33
![](https://img.aspxhome.com/file/2023/2/134432_0s.png)
python的debug实用工具 pdb详解
2021-04-16 10:31:17
基于JS实现html中placeholder属性提示文字效果示例
2023-09-07 22:50:58
![](https://img.aspxhome.com/file/2023/0/56130_0s.gif)
ASP用JAVASCRIPT脚本实现分页的办法
2007-10-30 13:18:00
windows下Mysql多实例部署的操作方法
2024-01-19 16:33:11
SQL- join多表关联问题
2024-01-28 06:22:48
![](https://img.aspxhome.com/file/2023/4/117804_0s.jpg)