python 线程之 threading(四)

时间:2020-12-30 23:04:37

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&lpar;三)

    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&lpar;二)

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

  4. python 线程之 threading&lpar;一&rpar;

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

  5. python 线程之&lowbar;thread

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

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

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

  7. python多线程之threading模块

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

  8. python多线程之Threading

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

  9. python多线程之threading、ThreadPoolExecutor&period;map

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

随机推荐

  1. 【干货分享】流程DEMO-制度发文和*任免

    流程名: 制度发文和*任免  业务描述: 当员工在该出勤的工作日出勤但漏打卡时,于一周内填写补打卡申请.  流程相关文件: 流程包.xml  流程说明: 直接导入流程包文件,即可使用本流程  表单: ...

  2. windows命令——taskkill

    C:\Users\Administrator>taskkill /? TASKKILL [/S system [/U username [/P [password]]]] { [/FI filt ...

  3. 应用程序框架实战三十七&colon;Util最新代码更新说明

    离上一篇又过去了一个月,时间比较紧,后续估计会更紧,所以这次将放出更多公共操作类及配套的CodeSmith模板,本篇将简要介绍新放出的重要功能,供有兴趣的同学参考. 重要更新 这一次对两个VS解决方案 ...

  4. thinkphp中curl的使用,常用于接口

    /lib/action/PublicAction.class.php class PublicAction extends Action{ //curl,返回数组 public function ge ...

  5. Android&lpar;安卓&rpar;系统USB-OTG-HID外设通讯开发

    正在为客户开发的X射线荧光成分分析仪(简称手持XRF分析仪 ),采用了MT6577 Android方案来开发.其中负责发送X射线的X-Ray-Tube设备是通过USB口来控制的.为了完成控制,就要求我 ...

  6. Jasper&lowbar;crosstab&lowbar;group &lowbar;Error incrementing crosstab dataset

    error detail: net.sf.jasperreports.engine.JRException: net.sf.jasperreports.engine.JRRuntimeExceptio ...

  7. ACdream: Sum

    Sum Time Limit: 2000/1000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others) SubmitStatisticN ...

  8. 【LeetCode】23&period; Merge k Sorted Lists

    合并k个已合并链表. 思路:先把链表两两合并,直到合并至只有一个链表 /** * Definition for singly-linked list. * struct ListNode { * in ...

  9. spring中使用Hibernate中的getCurrentSession报出:createQuery is not valid without active transaction

    1.错误信息 HTTP Status 500 - createQuery is not valid without active transaction type Exception report m ...

  10. 【响应式编程的思维艺术】 (3)flatMap背后的代数理论Monad

    目录 一. 划重点 二. flatMap功能解析 三. flatMap的推演 3.1 函数式编程基础知识回顾 3.2 从一个容器的例子开始 3.3 Monad登场 3.4 对比总结 3.5 一点疑问 ...