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 * ...
随机推荐
- fix
rounds the elements of A toward zero, resulting in an array of integers. For complex A, the imaginar ...
- solr string类型表示不支持分词
solr string类型表示不支持分词
- codeforces369A
Valera and Plates CodeForces - 369A Valera is a lazy student. He has m clean bowls and k clean plate ...
- BZOJ3425[POI2013]Polarization——DP+bitset+分块
题目描述 Everyone knew it would only be a matter of time. So what? Faced for years on, a peril becomes t ...
- Git初次使用总结,安装到上传代码,多平台[码云|github]
安装步骤 1.选择安装路径 2.选择创建图标,选择安装Git Bash和Git GUI 3.选择创建开始菜单 4.选择:use git and optional unix tools from the ...
- Codeforces Round #337 (Div. 2) C. Harmony Analysis
题目链接:http://codeforces.com/contest/610/problem/C 解题思路: 将后一个矩阵拆分为四个前一状态矩阵,其中三个与前一状态相同,剩下一个直接取反就行.还有很多 ...
- jupyter快捷键
jupyter快捷键(jupyter有两个模式,命令模式和编辑模式) 当前cell侧边为蓝色时,表示此时为命令模式,按Enter切换为编辑模式 当前cell侧边为绿色时,表示此时为编辑模式,按Esc切 ...
- 开源工作流程引擎ccflow多人待办处理模式的详解
多人待办工作处理模式,也是待办处理模式.是当接受的节点是多个人的时候,如何处理待办? 根据不用的场景,ccbpm把多人在普通节点下的处理模式分为如下几种. 抢办模式: A发送到B ,B节点上有n个人可 ...
- 自学Aruba3.2-Aruba配置架构-Virtual AP配置要点
点击返回:自学Aruba之路 自学Aruba3.2-Aruba配置架构-Virtual AP配置要点 1. AP.AP-Group和Virtual-AP的关系 解析列举: AP1.AP3, ...
- 【BZOJ1064】【NOI2008】假面舞会(图论,搜索)
题面 Description 一年一度的假面舞会又开始了,栋栋也兴致勃勃的参加了今年的舞会.今年的面具都是主办方特别定制的.每个参加舞会的人都可以在入场时选择一个自己喜欢的面 具.每个面具都有一个编号 ...