python多线程之Condition(条件变量)
2016-11-23 09:55
274 查看
#!/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 多线程学习05 条件变量 Condition
- 条件变量 condition 在 python 多线程编程中的应用
- Python 多线程 Condition 的使用
- python:threading多线程模块-Condition实现复杂的同步
- python3.4多线程实现同步的四种方式(锁机制、条件变量、信号量和同步队列)
- python笔记11-多线程之Condition(条件变量)
- Python 多线程 Condition 用法
- Python 线程(三):Condition(条件变量)
- python 多线程中的同步锁 Lock Rlock Semaphore Event Condition
- posix 多线程程序使用条件变量
- python模块之threading: 在python中使用多线程
- 在python中使用多线程下载网页
- java多线程之Condition的使用
- Python多线程知识点
- posix多线程程序使用条件变量的一个常见bug
- python中的多线程
- python多线程下载图片
- 使用Python多线程抓取并验证代理
- python多线程
- 简单的学习Python多线程