python 线程之 threading(三) http://www.cnblogs.com/someoneHan/p/6213100.html中对Event做了简单的介绍。

但是如果线程打算一遍一遍的重复通知某个事件。应该使用Condition

1. 使用Condition首先应该获取Condition即使Condition进入锁的状态

2. 在线程执行过程中需要等待其他线程通知,然后才开始向下运行的地方使用Condition.wait()方法,线程进入阻塞状态。

3. 使用Condition对锁进行release().

4. 如果Condition通知所有的等待线程继续运行可以使用notify_all()方法,如果只是唤醒其中的一个线程使用notify方法

 import threading
import time class CountDown(threading.Thread):
def __init__(self, startNum, condition):
self.condition = condition
self.startNum = startNum
threading.Thread.__init__(self) def run(self):
while self.startNum > 0:
with self.condition:
self.startNum -= 1
print('countdown current num :', self.startNum)
self.condition.wait() class CountUp(threading.Thread):
def __init__(self, startNum, condition):
self.condition = condition
self.startNum = startNum
threading.Thread.__init__(self) def run(self):
while self.startNum < 100:
with self.condition:
self.startNum += 1
print('countup current num:', self.startNum)
self.condition.wait() condition = threading.Condition()
countdown = CountDown(100, condition)
countdown.start()
countup = CountUp(0, condition)
countup.start()
for i in range(100):
with condition:
print('notify')
condition.notify_all()#如果只通知一个线程继续运行使用 condition.notify()
time.sleep(1)

python 线程之 threading(四)的更多相关文章

  1. python 线程之 threading(三)

    python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...

  2. python 线程之threading(五)

    在学习了Event和Condition两个线程同步工具之后还有一个我认为比较鸡肋的工具 semaphores 1. 使用semaphores的使用效果和Condition的notify方法的效果基本相 ...

  3. python 线程之 threading(二)

    在http://www.cnblogs.com/someoneHan/p/6204640.html 线程一中对threading线程的开启调用做了简单的介绍 1 在线程开始之后,线程开始独立的运行直到 ...

  4. python 线程之 threading(一)

    threading:基于对象和类的较高层面上的接口,threading模块在内部使用_thread模块来实现线程的对象以及常用的同步化工具的功能. 使用定制类的方式继承 threading.Threa ...

  5. python 线程之_thread

    python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...

  6. “死锁” 与 python多线程之threading模块下的锁机制

    一:死锁 在死锁之前需要先了解的概念是“可抢占资源”与“不可抢占资源”[此处的资源可以是硬件设备也可以是一组信息],因为死锁是与不可抢占资源有关的. 可抢占资源:可以从拥有他的进程中抢占而不会发生副作 ...

  7. python多线程之threading模块

    threading模块中的对象 其中除了Thread对象以外,还有许多跟同步相关的对象 threading模块支持守护线程的机制 Thread对象 直接调用法 import threading imp ...

  8. python多线程之Threading

    什么是线程? 线程是操作系统内核调度的基本单位,一个进程中包含一个或多个线程,同一个进程内的多个线程资源共享,线程相比进程是“轻”量级的任务,内核进行调度时效率更高. 多线程有什么优势? 多线程可以实 ...

  9. python多线程之threading、ThreadPoolExecutor.map

    背景: 某个应用场景需要从数据库中取出几十万的数据时,需要对每个数据进行相应的操作.逐个数据处理过慢,于是考虑对数据进行分段线程处理: 方法一:使用threading模块 代码: # -*- codi ...

随机推荐

  1. 团队博客作业- Week3

    成员介绍 韩青长  测试 我是韩青长,技术小白,抱着对软工的好奇和对未来工作的憧憬选了这门课.暂时选择了测试的工作,也对开发和UI有一定兴趣.从前上帝创造了我们,现在轮到我们来创造自己的软件了~ 陈彦 ...

  2. Promise deeply lookup

    Motivation Consider the following synchronous JavaScript function to read a file and parse it as JSO ...

  3. ng-show与ng-if区别

    <p>ng-show and ng-if : </p> <div ng-show="isShow">ng-show是否显示</div> ...

  4. wcf第4步之原生调用简单封装

    public interface IDemoServiceChannel : IDemoService, System.ServiceModel.IClientChannel { } public p ...

  5. Visual Studio 如何使用代码片段Code Snippet提高编程速度!!!

      使用Code Snippet简化Coding 在开发的项目的时候,你是否经常遇到需要重复编写一些类似的代码,比如是否经常会使用 for.foreach ? 在编写这两个循环语句的时候,你是一个字符 ...

  6. maven install Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default-war) on project web_nanchang

    maven打包成war时,报错:Failed to execute goal org.apache.maven.plugins:maven-war-plugin:2.1.1:war (default- ...

  7. [译]使用branch

    这篇文章将介绍Git分支. 首先, 看看如果创建分支, 这就像是request一个新的项目历史. 接着, 来看看git checkout是如果能被用来选择一个分支的. 最后, 学习用git merge ...

  8. 最短JavaScript判断是否为IE6、IE的方法

    常用的 JavaScript 检测浏览器为 IE 是哪个版本的代码,包括是否是最人极端厌恶的 ie6 识别与检测. var isIE=!!window.ActiveXObject; var isIE6 ...

  9. 【安卓】安卓res文件夹下的资源文件与R.java文件里面类的对应关系

    对于drawable.layout.menu文件夹下的每一个文件都分别会在R.java文件里面生成drawable.layout.menu类的一个常量,类名就是文件夹的名字,常量的名字就是文件名字. ...

  10. React 还是 Vue: 你应该选择哪一个Web前端框架?

    学还是要学的,用的多了,也就有更多的认识了,开发中遇到选择的时候也就简单起来了. 本文作者也做了总结: 如果你喜欢用(或希望能够用)模板搭建应用,请使用Vue    如果你喜欢简单和“能用就行”的东西 ...