python多线程之Condition(条件变量)
#!/usr/bin/env python # -*- coding: utf-8 -*- from threading import Thread, Condition import time items = [] condition = Condition() class Consumer(Thread): def __init__(self): Thread.__init__(self) def consume(self): global condition global items condition.acquire() if len(items) == 0: condition.wait() print ("Consumer notify: no item to consume") items.pop() print("Consumer notify: consumed 1 item") print("Consumer nofity: items to consume are "\ + str(len(items))) condition.notify() condition.release() def run(self): for i in range(0, 20): time.sleep(4) self.consume() class Producer(Thread): def __init__(self): Thread.__init__(self) def produce(self): global condition global items condition.acquire() if len(items) == 10: condition.wait() print ("Producer notify: item producted are"\ + str(len(items))) print("Producer nofity: stop the production!!") items.append(1) print("Producer nofity: total items producted "\ + str(len(items))) condition.notify() condition.release() def run(self): for i in range(0, 20): time.sleep(1) self.produce() if __name__ == "__main__": producer = Producer() consumer = Consumer() producer.start() consumer.start() producer.join() consumer.join()
python多线程之Condition(条件变量)的更多相关文章
- 27 python 初学(信号量、条件变量、同步条件、队列)
参考博客: www.cnblogs.com/yuanchenqi/articles/5733873.html semaphore 信号量: condition 条件变量: event 同步条件:条件 ...
- python笔记11-多线程之Condition(条件变量)
前言 当小伙伴a在往火锅里面添加鱼丸,这个就是生产者行为:另外一个小伙伴b在吃掉鱼丸就是消费者行为.当火锅里面鱼丸达到一定数量加满后b才能吃,这就是一种条件判断了. 这就是本篇要讲的Condition ...
- “死锁” 与 python多线程之threading模块下的锁机制
一:死锁 在死锁之前需要先了解的概念是“可抢占资源”与“不可抢占资源”[此处的资源可以是硬件设备也可以是一组信息],因为死锁是与不可抢占资源有关的. 可抢占资源:可以从拥有他的进程中抢占而不会发生副作 ...
- Condition条件变量
条件变量是一种比较复杂的线程同步机制 #!/usr/bin/env python # -*- coding: utf-8 -*- """ 条件变量,线程间通信提供的另一种 ...
- Python学习---线程锁/信号量/条件变量同步/线程池1221
线程锁 问题现象: 多线程情况下,CPU遇到阻塞会进行线程的切换,所以导致执行了tmp-=1的值还未赋值给num=tmp,另一个线程2又开始了tmp -=1,所以导致最后的值重复赋值给了num,所以出 ...
- Python:Day29 信号量、条件变量
信号量:semaphore 信号量是用来控制线程并发数的.(理解:虽然GIL任意时刻都只有一个线程被执行,但是所有线程都有资格去抢,semaphore就是用来控制抢的GIL的数量,只有获取了semap ...
- java Condition条件变量的通俗易懂解释、基本使用及注意点
最近在看pthread方面的书,看到条件变量一节的时候,回忆了下java中条件变量的使用方式. java中条件变量都实现了java.util.concurrent.locks.Condition接口, ...
- linux网络编程之posix条件变量
今天来学习posix的最后一个相关知识----条件变量,言归正传. 下面用一个图来进一步描述条件变量的作用: 为什么呢? 这实际上可以解决生产者与消费者问题,而且对于缓冲区是无界的是一种比较理解的解决 ...
- python线程之condition
cond = threading.Condition() # 类似lock.acquire() cond.acquire() # 类似lock.release() cond.release() # 等 ...
随机推荐
- div 加滚动条
div 加滚动条的方法: <div style="position:absolute; height:400px; overflow:auto"></div> ...
- Delphi Dll 消息处理
转载:http://blog.csdn.net/lailai186/article/details/8770643 事情的导火线是GIF图片的显示. 在应用程序中, 利用三方的GIFImage.pas ...
- 大数据热点问题TOP K
1单节点上的topK (1)批量数据 数据结构:HashMap, PriorityQueue 步骤:(1)数据预处理:遍历整个数据集,hash表记录词频 (2)构建最小堆:最小堆只存k个数据. 时间复 ...
- 3分钟,9个Q&A让你快速知道Docker到底是什么
不论是Google.Amazon.Microsoft.VMware都纷纷拥戴,加入Docker和Container所掀起的新时代云端虚拟化行列,这两项技术成为了IT界的新趋势.Docker和Conta ...
- ubuntu14.04 archive sources.list
deb http://archive.ubuntu.com/ubuntu/ trusty main restricted universe multiverse deb http://archive. ...
- find参数exec、管道符|、xargs的区别
1.这三个命令都可以将前面的输出做为后面的输入. 2.他们对于前面的输出,处理方式不同. find . -name "*.sh" -exec cat {} \; find . -n ...
- Mybatis中的in查询和foreach标签
Mybatis中的foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合. foreach元素的属性主要有 item,index,collection,open,separato ...
- ajax删除数据后刷新页面
代码如图: 理解: 发送请求后删除name 属性为 name 的data;用rem .remove删除: 删除后找到页面上app的数量:定义page = len/12+1; 找到当前页是哪一页,如果l ...
- VB中字符串操作函数
Len Len(string|varname) 返回字符串内字符的数目,或是存储一变量所需的字节数. Trim Trim(string) 将字符串前后的空格去掉 Ltrim Ltrim(string) ...
- Linux rpm 命令参数使用详解[介绍和应用]
RPM是RedHat Package Manager(RedHat软件包管理工具)类似Windows里面的“添加/删除程序” rpm 执行安装包 二进制包(Binary)以及源代码包(Source)两 ...