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 * ...
随机推荐
- 【转】CNN卷积神经网络_ GoogLeNet 之 Inception(V1-V4)
http://blog.csdn.net/diamonjoy_zone/article/details/70576775 参考: 1. Inception[V1]: Going Deeper with ...
- 荣耀实锤Magic2或将助力AI,再次带动成长?
临近年底,热闹了一年的手机圈纷纷偃旗息鼓,准备为明年3月的新品发力.然而今天(12月7日),恰逢节气大雪,@荣耀手机 在微博发布了一张预热海报,随后荣耀总裁赵明转发这条微博表示「关于技术,真的有很多话 ...
- VMware Linux虚拟机yum源更换成国内阿里源
[虚拟机系统] Centos 6.8 阿里源:https://opsx.alibaba.com/mirror 网易源:http://mirrors.163.com/ 更换yum源时请保证虚拟机和外网是 ...
- GO系列教程
1.介绍与安装 2.Hello World 3.变量 4. 类型 5.常量 6.函数(Function) 7.包 8.if-else 语句 9.循环 10.switch语句 11.数组和切片 12.可 ...
- Problem B. Harvest of Apples HDU - 6333(莫队)
Problem Description There are n apples on a tree, numbered from 1 to n.Count the number of ways to p ...
- day22 collection 模块 (顺便对比queue也学习了一下队列)
collection 定义命名元祖,让元祖的每个元素可以通过类似对象属性的方法用".属性"及其方便的取值. 定义可前后拿取值且可迭代的双端队列 定义有顺序的字典 定义有默认值的字典 ...
- 通俗理解TCP/IP协议三次握手四次分手流程
转自:https://blog.csdn.net/special23/article/details/54137298 三次握手流程 客户端发个请求“开门呐,我要进来”给服务器 服务器发个“进来吧,我 ...
- MT【235】两道函数题
已知$g(x)=x^2-ax+4a$,记$h(x)=|\dfrac{x}{g(x)}|$,若$h(x)$在$(0,1]$上单调递增,求$a$的取值范围. 解答: 已知$$g(x)=\begin{cas ...
- BZOJ 193题纪念
- java date总结
Java 8 中 Date与LocalDateTime.LocalDate.LocalTime互转 Java 8中 java.util.Date 类新增了两个方法,分别是from(Instant ...