python Condition
import threading
# 必须要使用condition的例子
# class XiaoAi(threading.Thread):
# def __init__(self, lock):
# super().__init__(name="小爱")
# self.lock = lock
#
# def run(self):
# self.lock.acquire()
# print("{} : 在 ".format(self.name))
# self.lock.release()
#
# self.lock.acquire()
# print("{} : 好啊 ".format(self.name))
# self.lock.release()
#
# class TianMao(threading.Thread):
# def __init__(self, lock):
# super().__init__(name="天猫精灵")
# self.lock = lock
#
# def run(self):
#
# self.lock.acquire()
# print("{} : 小爱同学 ".format(self.name))
# self.lock.release()
#
# self.lock.acquire()
# print("{} : 我们来对古诗吧 ".format(self.name))
# self.lock.release()
class XiaoAi(threading.Thread):
def __init__(self, cond):
super().__init__(name="小爱")
self.cond = cond def run(self):
with self.cond:
self.cond.wait()
print("{} : 在 ".format(self.name))
self.cond.notify() self.cond.wait()
print("{} : 好啊 ".format(self.name))
self.cond.notify() self.cond.wait()
print("{} : 君住长江尾 ".format(self.name))
self.cond.notify() self.cond.wait()
print("{} : 共饮长江水 ".format(self.name))
self.cond.notify() self.cond.wait()
print("{} : 此恨何时已 ".format(self.name))
self.cond.notify() self.cond.wait()
print("{} : 定不负相思意 ".format(self.name))
self.cond.notify() class TianMao(threading.Thread):
def __init__(self, cond):
super().__init__(name="天猫精灵")
self.cond = cond def run(self):
with self.cond:
print("{} : 小爱同学 ".format(self.name))
self.cond.notify()
self.cond.wait() print("{} : 我们来对古诗吧 ".format(self.name))
self.cond.notify()
self.cond.wait() print("{} : 我住长江头 ".format(self.name))
self.cond.notify()
self.cond.wait() print("{} : 日日思君不见君 ".format(self.name))
self.cond.notify()
self.cond.wait() print("{} : 此水几时休 ".format(self.name))
self.cond.notify()
self.cond.wait() print("{} : 只愿君心似我心 ".format(self.name))
self.cond.notify()
self.cond.wait() if __name__ == "__main__":
cond = threading.Condition()
xiaoai = XiaoAi(cond)
tianmao = TianMao(cond) #启动顺序很重要
#在调用with cond之后才能调用wait或者notify方法
xiaoai.start()
tianmao.start()
condition有两层锁:
1. 一把底层锁会在线程调用了wait方法的时候释放(是先创建一把锁(这就是第二把锁),然后再release),底层还是Lock或者RLock,
2.第二把锁会在每次调用wait的时候创建一把新的,并放入到cond的等待队列中(采用的是dqueue),一直acquire,等到notify方法的唤醒。notify方法会出队一把锁,这把锁就是刚刚创建的锁,然后release。
python Condition的更多相关文章
- python Condition类(锁)
Condition(条件变量)通常与一个锁关联.需要在多个Contidion中共享一个锁时,可以传递一个Lock/RLock实例给构造方法,否则它将自己生成一个RLock实例. 不理解锁的,请看上一条 ...
- Python状况:为什么PyPy是Python的未来?
Python 现在已经不仅仅是胶水脚本语言了. 不信?看看下面使用Python的成功案例: YouTube - 主要由 Python编写 NASA Industrial Light & Mag ...
- python 全栈开发,Day81(博客系统个人主页,文章详情页)
一.个人主页 随笔分类 需求:查询当前站点每一个分类的名称以及对应的文章数 完成这个需求,就可以展示左侧的分类 它需要利用分组查询,那么必须要会基于双下划线的查询. 基于双下划线的查询,简单来讲,就是 ...
- Python threads synchronization: Locks, RLocks, Semaphores, Conditions, Events and Queues(Forwarding)
This article describes the Python threading synchronization mechanisms in details. We are going to s ...
- python常用技巧 — 杂
目录: 1. 找到字符串中的所有数字(python find digits in string) 2. python 生成连续的浮点数(如 0.1, 0.2, 0.3, 0.4, ... , 0.9) ...
- python多线程之Condition(条件变量)
#!/usr/bin/env python # -*- coding: utf-8 -*- from threading import Thread, Condition import time it ...
- PYTHON线程知识再研习E---条件变量同步Condition
Python提供的Condition对象提供了对复杂线程同步问题的支持.Condition被称为条件变量,除了提供与Lock类似的 acquire和release方法外,还提供了wait和notify ...
- 基于condition 实现的线程安全的优先队列(python实现)
可以把Condiftion理解为一把高级的琐,它提供了比Lock, RLock更高级的功能,允许我们能够控制复杂的线程同步问题.threadiong.Condition在内部维护一个琐对象(默认是RL ...
- {Python之线程} 一 背景知识 二 线程与进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 七 python与线程 八 Threading模块 九 锁 十 信号量 十一 事件Event 十二 条件Condition(了解) 十三 定时器
Python之线程 线程 本节目录 一 背景知识 二 线程与进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 七 python与线程 八 Thr ...
随机推荐
- 常用regex正则表达式
[单个]字符匹配 任意字符:表示由任意字符组成 \\:匹配一个'\' \n:匹配换行 \t:匹配制表符 [单个]字符集(可以从里面任选一个字符) [abc]:可以是字母abc中任意一个 [^abc]: ...
- three.js实现世界3d地图
概况如下: 1.THREE.Shape绘制世界地图平面地图: 2.THREE.ExtrudeGeometry将绘制的平面沿着Z轴拉伸,实现3d效果: 效果图如下: 预览地址:three.js实现世界3 ...
- MySQL(9)---纪录一次实际开发过程中用到的复杂存储过程
Mysql(9)---纪录一次实际开发过程中用到的复杂存储过程 为了尽可能的还原当时为什么需要用到存储过程,下面我写了个详细的文档,我们可以从需求文档出发来分析. 有关存储过程之前也写了两篇文章来做铺 ...
- 深入理解JVM,7种垃圾收集器
本人免费整理了Java高级资料,一共30G,需要自己领取.传送门:https://mp.weixin.qq.com/s/JzddfH-7yNudmkjT0IRL8Q 如果说收集算法是内存回收的方法论, ...
- CSS filter滤镜试玩
1.模糊(blur). 用法:给相应元素设置高斯模糊,传入的px数值越大越模糊. 2.亮度(brightness). 用法:给元素设置亮度,0%为全黑,100%为元素原始亮度,>100%表示会比 ...
- 使QScrollArea的背景透明,并且不影响子控件
使QScrollArea的背景透明,乍看很easy,其实却是一个目前百度上百不出来的问题; 最容易想到的,用qss: scrollArea->setStyleSheet("backgr ...
- js的运用
JS数组的4种声明方法: //第一种 ,,,,] console.log(A) //第二种 var B=new Array(); B[]= B[]= B[]= console.log(B)//第三种 ...
- [b0003] 总览:Hadoop 个人学习路线进展
3. Spark 搭建 过 1.1 搭建伪分布式2.0.1 ok 2016-10-23 耗时 2h ref [0006] Spark 2.0.1 伪分布式搭建练手 后续: 1.2 分布 ...
- Docker Harbor
简介: Harbor是一个用于存储Docker镜像的企业级Registry服务:本章将介绍如何搭建Harbor Registry! Github官方安装文档: https://github.com/g ...
- appium---uiautomator定位方法
前面总结了7种定位方法,今天在介绍一种uiautomator方法,其实appium就是基于uiautomator框架实现的,让我们一起看下uiautomator有哪些定位方法可以使用 uiautoma ...