python线程之condition
cond = threading.Condition()
# 类似lock.acquire()
cond.acquire()
# 类似lock.release()
cond.release()
# 等待指定触发,同时会释放对锁的获取,直到被notify才重新占有琐。
cond.wait()
# 发送指定,触发执行
cond.notify()
以下分别为两个线程,三个线程,四个线程。可直接看四个线程的运行过程,更加直观,有时候用文字解释不如直接实验让人理解的快。
import threading,time cond = threading.Condition()
#cond.acquire()
#cond.release()
#cond.wait()
#cond.notify()
def aa():
print ('thread_aa get')
cond.acquire()
time.sleep(5)
print ('thread_aa finished first job')
cond.wait()#释放对锁的控制,好让别的进程acquire到
#同时阻塞在此,等待得到锁的那个进程的先cond.notify后(cond.wait或cond.release)
print ('thread_aa get again')
time.sleep(5)
print ('thread_aa finished all work')
cond.notify()#告诉刚才释放锁的那个进程等通知吧,等我要么wait要么release
cond.release()#释放锁
def bb():
cond.acquire()
print ('thread_bb get')
cond.notify()#告诉刚才释放锁的那个进程等通知吧,等我要么wait要么release
print ('依然是我bb在运行')
time.sleep(5)
print ('thread_bb finished first job')
cond.wait()#释放对锁的控制,而后被阻塞的aa就可以执行了
#同时阻塞在此,等待得到锁的aa的先cond.notify后(cond.wait或cond.release)
print ('thread_aa get again')
print ('thread_bb finished all works')
cond.release() a=threading.Thread(target=aa)
a.start()
time.sleep(2)
b=threading.Thread(target=bb)
b.start()
import threading,time cond = threading.Condition()
def aa():
cond.acquire()
time.sleep(1)
print ('aa1')
cond.wait()
time.sleep(1)
print ('aa2')
cond.notify()
cond.wait()
print ('aa3')
cond.notify()
cond.wait()
print ('aa4')
cond.release()
def bb():
cond.acquire()
print ('bb1')
cond.wait()#没notify而直接wait,就是将对a的控制权抛出,但是自己的控
# 制权还在a那里,任意一个 acquire到锁的进城将得到对a的控
# 制权(除非他自己又抛出),而这个acquire到锁的进程的控制
# 权在这个进程,如果这个进程在之后退出了,那么他控制的
# 进程的控制权将转交给控制他的进程 print ('bb2')
cond.notify()
cond.release()
def cc():
cond.acquire()
print ('cc1')
cond.notify()
cond.wait()
print ('cc2')
cond.notify()
cond.wait()
print ('cc3')
cond.notify()
cond.release()
a=threading.Thread(target=aa)
a.start()
time.sleep(2)
b=threading.Thread(target=bb)
b.start()
time.sleep(2)
c=threading.Thread(target=cc)
c.start() '''这是三个以上版本的,或许更加直观。运行结果:
aa1
bb1
cc1
aa2
bb2
cc2
aa3
cc3
aa4
'''
import threading,time cond = threading.Condition()
def aa():
cond.acquire()
time.sleep(1)
print ('aa1')
cond.wait()
time.sleep(1)
print ('aa2')
cond.wait()
print ('aa3')
cond.notify()
cond.wait()
print ('aa4')
cond.release()
def bb():
cond.acquire()
print ('bb1')
cond.wait() print ('bb2')
cond.notify()
cond.release()
def cc():
cond.acquire()
print ('cc1')
cond.notify()
cond.wait()
print ('cc2')
cond.notify()
cond.wait()
print ('cc3')
cond.notify()
cond.release()
def dd():
cond.acquire()
print ('dd1')
cond.notify()
cond.release()
a=threading.Thread(target=aa)
a.start()
time.sleep(2)
b=threading.Thread(target=bb)
b.start()
time.sleep(2)
c=threading.Thread(target=cc)
c.start()
time.sleep(2)
d=threading.Thread(target=dd)
d.start() '''这是4个版本的,运行结果:
aa1
bb1
cc1
aa2
dd1
bb2
cc2
aa3
cc3
aa4
'''
python线程之condition的更多相关文章
- python 线程之 threading(四)
python 线程之 threading(三) http://www.cnblogs.com/someoneHan/p/6213100.html中对Event做了简单的介绍. 但是如果线程打算一遍一遍 ...
- python 线程之 threading(三)
python 线程之 threading(一)http://www.cnblogs.com/someoneHan/p/6204640.html python 线程之 threading(二)http: ...
- python 线程之_thread
python 线程之_thread _thread module: 基本用法: def child(tid): print("hello from child",tid) _thr ...
- python多线程之Condition(条件变量)
#!/usr/bin/env python # -*- coding: utf-8 -*- from threading import Thread, Condition import time it ...
- python笔记11-多线程之Condition(条件变量)
前言 当小伙伴a在往火锅里面添加鱼丸,这个就是生产者行为:另外一个小伙伴b在吃掉鱼丸就是消费者行为.当火锅里面鱼丸达到一定数量加满后b才能吃,这就是一种条件判断了. 这就是本篇要讲的Condition ...
- 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 ...
- iOS多线程之8.NSOPeration的其他用法
本文主要对NSOPeration的一些重点属性和方法做出介绍,以便大家可以更好的使用NSOPeration. 1.添加依赖 - (void)addDependency:(NSOperation * ...
随机推荐
- httprequest存储的是字符内容 而文本内容是以字节形式上传的;所以普通的取值方式无法从httprequest取到值
httprequest存储的是字符内容 而文本内容是以字节形式上传的;所以普通的取值方式无法从httprequest取到值
- linux 环境下 firefox乱码问题解决
https://blog.csdn.net/wlwlwlwl015/article/details/51482065
- pgm16
前面结束了关于 learning 部分一些粗浅的讨论,我们大概明白了一些 learning 中 common sense/techniques.剩下的部分我们分为 causality 和 utilit ...
- BZOJ4259 残缺的字符串(FFT)
两个串匹配时相匹配的位置位置差是相同的,那么翻转一个串就变成位置和相同,卷积的形式. 考虑如何使用卷积体现两个位置能否匹配.一个暴力的思路是每次只考虑一种字符,将其在一个串中设为1,并在另一个串中将不 ...
- Docker报错总结
[Docker push镜像报错] The push refers to a repository [192.168.200.103:5000/rancher/server]Get https://1 ...
- MT【33】证明琴生不等式
解答:这里数学归纳法证明时指出关键的变形. 评:撇开琴生不等式自身的应用和意义外,单单就这个证明也是一道非常不错的练习数学归纳法的经典题目.
- 【BZOJ2245】[SDOI2011]工作安排(费用流)
[BZOJ2245][SDOI2011]工作安排(费用流) 题面 BZOJ 洛谷 题解 裸的费用流吧. 不需要拆点,只需要连边就好了,保证了\(W_j<W_{j+1}\). #include&l ...
- 更新本地git仓库的远程地址(remote地址)
如果远程仓库的地址更新了,我们本地仓库就需要更新remote地址, 可以通过git remote -v或者cat .git/config查看通信方式及远程地址 更新远程地址的方式有两种: 第一种方式: ...
- [luogu1967][货车运输]
题目链接 题意: 其实题目的意思就是问从x到y权值最小的路的权值最大能是多少. 思路: 首先可以先把这张图变成一棵树.因为那些更小的点肯定是不跑更优秀,而且题目没有要求路程,所以生成一棵树,只要能保证 ...
- javascript:window.location.replace 与 window.location.reload() 刷新页面的不同效果
今天早上我发现一个问题,当一个网页的地址最后面是一个#时(比如:http://www.baidu.com/go.asp#), 执行:window.location.replace(window.loc ...