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的更多相关文章

  1. python Condition类(锁)

    Condition(条件变量)通常与一个锁关联.需要在多个Contidion中共享一个锁时,可以传递一个Lock/RLock实例给构造方法,否则它将自己生成一个RLock实例. 不理解锁的,请看上一条 ...

  2. Python状况:为什么PyPy是Python的未来?

    Python 现在已经不仅仅是胶水脚本语言了. 不信?看看下面使用Python的成功案例: YouTube - 主要由 Python编写 NASA Industrial Light & Mag ...

  3. python 全栈开发,Day81(博客系统个人主页,文章详情页)

    一.个人主页 随笔分类 需求:查询当前站点每一个分类的名称以及对应的文章数 完成这个需求,就可以展示左侧的分类 它需要利用分组查询,那么必须要会基于双下划线的查询. 基于双下划线的查询,简单来讲,就是 ...

  4. 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 ...

  5. python常用技巧 — 杂

    目录: 1. 找到字符串中的所有数字(python find digits in string) 2. python 生成连续的浮点数(如 0.1, 0.2, 0.3, 0.4, ... , 0.9) ...

  6. python多线程之Condition(条件变量)

    #!/usr/bin/env python # -*- coding: utf-8 -*- from threading import Thread, Condition import time it ...

  7. PYTHON线程知识再研习E---条件变量同步Condition

    Python提供的Condition对象提供了对复杂线程同步问题的支持.Condition被称为条件变量,除了提供与Lock类似的 acquire和release方法外,还提供了wait和notify ...

  8. 基于condition 实现的线程安全的优先队列(python实现)

    可以把Condiftion理解为一把高级的琐,它提供了比Lock, RLock更高级的功能,允许我们能够控制复杂的线程同步问题.threadiong.Condition在内部维护一个琐对象(默认是RL ...

  9. {Python之线程} 一 背景知识 二 线程与进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 七 python与线程 八 Threading模块 九 锁 十 信号量 十一 事件Event 十二 条件Condition(了解) 十三 定时器

    Python之线程 线程 本节目录 一 背景知识 二 线程与进程的关系 三 线程的特点 四 线程的实际应用场景 五 内存中的线程 六 用户级线程和内核级线程(了解) 七 python与线程 八 Thr ...

随机推荐

  1. 安装keepalived OpenSSL is not properly installed on your system. !!!

    错误信息: configure: error: !!! OpenSSL is not properly installed on your system. !!! !!! Can not includ ...

  2. oracle学习笔记(二十二) REF 动态游标

    动态游标 定义语法 --声明 $cursor_name$ sys_refcursor --打开动态游标 open $cursor_name$ is 查询语句; --关闭游标 close $cursor ...

  3. C# - 操作Word文档小实验

    前言 本篇主要记录:VS2019 WinFrm桌面应用程序实现对Word文档的简单操作. 准备工作 搭建WinFrm前台界面 添加必要的控件,如下图 NuGet包管理器 安装Microsoft.Off ...

  4. sql server 查询出整数 (可灵活运用)

    系统辅助用表,常用来获取数字,当然还有其他用途

  5. MySQL学习——操作表

    MySQL学习——操作表 摘要:本文主要学习了使用DDL语句操作表的方法. 创建表 语法 create table 表名 [表定义选项] [表选项]; 表定义选项 用来创建定义表的结构,由列名(col ...

  6. arcgis api 3.x for js 解决 textSymbol 文本换行显示(附源码下载)

    前言 关于本篇功能实现用到的 api 涉及类看不懂的,请参照 esri 官网的 arcgis api 3.x for js:esri 官网 api,里面详细的介绍 arcgis api 3.x 各个类 ...

  7. Web安全攻防笔记-SQL注入

    information_schema(MySQL5.0版本之后,MySQL数据库默认存放一个information_schema数据库) information_schema的三个表: SCHEMAT ...

  8. xcode 左边导航栏中,类文件后面的标记“A”,"M","?"……等符号的含义???

        "M" = Locally modified     "U" = Updated in repository   "A" = Loc ...

  9. Python—基础之杂货铺

    列表.元组.字典之前的互相转换 列表与元组的转换 # 列表转换成元组:使用 tuple 函数 num_list = [3, 6, 9] num_tuple = tuple(num_list) prin ...

  10. C学习笔记(2)---各类函数

    1.函数(function)声明定义: 见例子,不复述:https://www.runoob.com/cprogramming/c-functions.html 2. 函数参数(Parameters“ ...