python 线程之 threading(四)
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(四)的更多相关文章
- python 线程之 threading(三)
python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...
- python 线程之threading(五)
在学习了Event和Condition两个线程同步工具之后还有一个我认为比较鸡肋的工具 semaphores 1. 使用semaphores的使用效果和Condition的notify方法的效果基本相 ...
- python 线程之 threading(二)
在http://www.cnblogs.com/someoneHan/p/6204640.html 线程一中对threading线程的开启调用做了简单的介绍 1 在线程开始之后,线程开始独立的运行直到 ...
- python 线程之 threading(一)
threading:基于对象和类的较高层面上的接口,threading模块在内部使用_thread模块来实现线程的对象以及常用的同步化工具的功能. 使用定制类的方式继承 threading.Threa ...
- python 线程之_thread
python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...
- “死锁” 与 python多线程之threading模块下的锁机制
一:死锁 在死锁之前需要先了解的概念是“可抢占资源”与“不可抢占资源”[此处的资源可以是硬件设备也可以是一组信息],因为死锁是与不可抢占资源有关的. 可抢占资源:可以从拥有他的进程中抢占而不会发生副作 ...
- python多线程之threading模块
threading模块中的对象 其中除了Thread对象以外,还有许多跟同步相关的对象 threading模块支持守护线程的机制 Thread对象 直接调用法 import threading imp ...
- python多线程之Threading
什么是线程? 线程是操作系统内核调度的基本单位,一个进程中包含一个或多个线程,同一个进程内的多个线程资源共享,线程相比进程是“轻”量级的任务,内核进行调度时效率更高. 多线程有什么优势? 多线程可以实 ...
- python多线程之threading、ThreadPoolExecutor.map
背景: 某个应用场景需要从数据库中取出几十万的数据时,需要对每个数据进行相应的操作.逐个数据处理过慢,于是考虑对数据进行分段线程处理: 方法一:使用threading模块 代码: # -*- codi ...
随机推荐
- 【团队项目选题】自选项目:桌游APP
由于我们团队的黄金点游戏结果是第二名,按理说是一定能选到一个自选项目的,所以以下只列出我们选择的自选项目 自选项目名称 桌游APP 暂定 (名字还没想好) 项目大致介绍 此APP集成了多种桌游,主 ...
- TP-LINK WR941 DD-WRT刷回OpenWRT及OpenWRT刷回原厂固件
1.DD-Wrt 刷回 OpenWrt A.从官网下载固件: root@TL-DDWRT:/tmp# wget http://downloads.openwrt.org/barrier_breaker ...
- Java中 NIO与IO的区别
当学习了Java NIO和IO的API后,一个问题马上涌入脑海: 我应该何时使用IO,何时使用NIO呢?在本文中,我会尽量清晰地解析Java NIO和IO的差异.它们的使用场景,以及它们如何影响您的代 ...
- Example: Encoded SNMP Message - SNMP Tutorial
30.11 Example Encoded SNMP Message The encoded form of ASN.1 uses variable-length fields to represen ...
- 10月30日下午 PHP精确查询(模糊查询、模糊+关键字共同查询)
1.一个条件的模糊查询 <body> <br /> <form action="main.php" method="post"&g ...
- JSon 对象转字符的一些方法
引用System.Web.Entity.dll public static string ToJSON(this object obj) { JavaScriptSerializer serializ ...
- PHP中的变量与常量详解
几乎所有的编程语言都会涉及到变量和常量这两个概念,PHP也不例外.本节将介绍PHP语言中的变量和常量的应用方法. 一.什么是变量和常量 在程序执行的过程中,变量存储的值可以随时改变,而常量存储的值是不 ...
- PHP中global与$GLOBALS['']的区别
+++ 探讨(一)+++++++++++++++++++++++++++++++++++++++ 很多人都认为global和$GLOBALS[]只是写法上面的差别,其实不然. 根据官方的解释是 $GL ...
- 【Android学习】android:layout_weight的用法实例
对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...
- 06OC之内存管理
在高级语言中,例如C#是通过垃圾回收机制(GC)来解决这个问题,但是在OC并没有类似的垃圾回收机制,因此必须由程序员手动去维护.今天就讲讲OC中的内存管理: 一.内存管理原理 在Xcode4.2之后的 ...