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. 七牛图片上传JSSDK

    BASE64图片上传 接口说明: POST /putb64/<Fsize>/key/<EncodedKey>/mimeType/<EncodedMimeType>/ ...

  2. 有用的git建议

    这篇文章的目的是给经常使用git管理项目提供一个有益的提醒.如果你是git新手,可以先阅读文后的引用部分,然后在回头阅读此篇文章.在介绍git命令之前,你可以先看看来自 on-my-zsh 提供的别名 ...

  3. <<< struts 的一系列介绍

    struts有什么用? 以前使用servlet开发应用系统的人深深感受到在java代码中嵌入大量html代码是一件非常痛苦的事,于是sun推出了JSP,解决了java代码中嵌入html代码的问题.但是 ...

  4. c++ 虚函数

    class A { public: virtual void f();//希望派生类重写 void fun();//绝大多数情况下不要重新定义基类的非虚函数,那样会打破公有继承Is-A的关系,而且行为 ...

  5. git学习之旅

    http://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000/0013743256916071d ...

  6. 生成彩条的MATLAB代码

    clc;close all;clear %read image % RGBimga = imread('bmpinput_1080p.bmp'); RGBimga = imread('bmpinput ...

  7. 转载:postgresql分区与优化

    --对于分区表constraint_exclusion 这个参数需要配置为partition或on postgres=# show constraint_exclusion ; constraint_ ...

  8. C# 操作office知识点汇总

    1. C#操作Word的超详细总结

  9. C和指针 第十章 结构和联合 习题

    1. 记账信息结构联合 typedef struct { unsigned int areaNum; unsigned int transNum; unsigned int station; } ph ...

  10. 分享一个Visual Studio的背景插件,让堆码更富情趣

    忘记一件重要的事情,我使用的是VS 2012版,其他更高版本应该是可以找到的,以下版本就不清楚了.有可能找不到,见谅,也不是我开发的,只是偶尔碰到,拿出来让大家知道. 上周某日,新生命群里面还是一如既 ...