python多线程之Event(事件)
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import time
from threading import Thread, Event
import random
items = []
event = Event()
class Consumer(Thread):
def __init__(self, items, event):
Thread.__init__(self)
self.items = items
self.event = event
def run(self):
while True:
time.sleep(2)
self.event.wait()
item = self.items.pop()
print("Consumer notify: %d poped from list by %s"
%(item, self.name))
class Producer(Thread):
def __init__(self, items, event):
Thread.__init__(self)
self.items = items
self.event = event
def run(self):
global item
for i in range(10):
time.sleep(2)
item = random.randint(0, 256)
self.items.append(item)
print ("Producer nofity: item N %d appended to list by %s"
% (item, self.name))
print ("Producer notify: event set by %s "
% self.name)
self.event.set()
print("Produce notify: event clear by %s\n"
% self.name)
self.event.clear()
if __name__ == "__main__":
producer = Producer(items, event)
consumer = Consumer(items, event)
producer.start()
consumer.start()
producer.join()
consumer.join()

python多线程之Event(事件)的更多相关文章
- 并发编程之Event事件
Event事件 用来同步线程之间的状态. 举个例子: 你把一个任务丢到了子线程中,这个任务将异步执行.如何获取到这个任务的执行状态 解决方法: 如果是拿到执行结果 我们可以采用异步回调, 在这里我 ...
- “死锁” 与 python多线程之threading模块下的锁机制
一:死锁 在死锁之前需要先了解的概念是“可抢占资源”与“不可抢占资源”[此处的资源可以是硬件设备也可以是一组信息],因为死锁是与不可抢占资源有关的. 可抢占资源:可以从拥有他的进程中抢占而不会发生副作 ...
- python多线程之threading模块
threading模块中的对象 其中除了Thread对象以外,还有许多跟同步相关的对象 threading模块支持守护线程的机制 Thread对象 直接调用法 import threading imp ...
- python多线程之Condition(条件变量)
#!/usr/bin/env python # -*- coding: utf-8 -*- from threading import Thread, Condition import time it ...
- python多线程之semaphore(信号量)
#!/usr/bin/env python # -*- coding: utf-8 -*- import threading import time import random semaphore = ...
- python多线程之t.setDaemon(True) 和 t.join()
0.目录 1.参考2.结论 (1)通过 t.setDaemon(True) 将子线程设置为守护进程(默认False),主线程代码执行完毕后,python程序退出,无需理会守护子线程的状态. ...
- python多线程之Threading
什么是线程? 线程是操作系统内核调度的基本单位,一个进程中包含一个或多个线程,同一个进程内的多个线程资源共享,线程相比进程是“轻”量级的任务,内核进行调度时效率更高. 多线程有什么优势? 多线程可以实 ...
- python多线程之threading、ThreadPoolExecutor.map
背景: 某个应用场景需要从数据库中取出几十万的数据时,需要对每个数据进行相应的操作.逐个数据处理过慢,于是考虑对数据进行分段线程处理: 方法一:使用threading模块 代码: # -*- codi ...
- python 线程之 threading(四)
python 线程之 threading(三) http://www.cnblogs.com/someoneHan/p/6213100.html中对Event做了简单的介绍. 但是如果线程打算一遍一遍 ...
随机推荐
- TP框架自动加载优先级
$map = array('Think\Log'=>THINK_PATH.'Think\Log.php','Org\Util\Array'=>THINK_PATH.'Org\Util\Ar ...
- PHP 中的行为 ,与什么是切面
行为(Behavior)扩展以及插件(Plug or Hook)详解: 行为(Behavior)是ThinkPHP扩展机制中比较关键的一项扩展,行为即可以独立调用,也可以绑定到某个 标签中进行监听,官 ...
- linux kernel 字符设备详解
有关Linux kernel 字符设备分析: 参考:http://blog.jobbole.com/86531/ 一.linux kernel 将设备分为3大类,字符设备,块设备,网络设备. 字符设备 ...
- 16 BasicHashTable基本哈希表类(三)——Live555源码阅读(一)基本组件类
这是Live555源码阅读的第一部分,包括了时间类,延时队列类,处理程序描述类,哈希表类这四个大类. 本文由乌合之众 lym瞎编,欢迎转载 http://www.cnblogs.com/oloroso ...
- django初始
创建django工程 django-admin startproject [工程名称] mysite - mysite # 对整个程序进行配置 - init - settings # 配置文件 - u ...
- 再谈Weiphp公众平台开发——1、增加插件
去年开始接触基于Weiphp的公众平台开发,一直没时间好好整理一下. 下面开始讲解第一个自定义weiphp插件:MyHello的开发流程. 1.插件创建.在weiphp管理后台依次点击“插件管理-&g ...
- SQL关于分页的sql查询语句 limit 和row_number() OVER函数
在做项目的时候需要些分页,用的数据库是mysql,之前看到的参考例子是用MS SQL做的,在MS SQL.ORACLE里面有ROW_NUMBER() OVER函数可以在数据库里对数据进行分组.百度后的 ...
- 【GoLang】tcmalloc && jemalloc
https://www.douban.com/note/512625720/ http://blog.csdn.net/hanxin1987216/article/details/8156010 ht ...
- Silverlight 中datagrid控件-- 通过设置数据虚拟化加速显示
定义依赖属性作为datagrid的数据源 protected static readonly DependencyProperty ViewLogsProperty = DependencyPrope ...
- mybatis Result Maps collection already contains value for com.ebways.dictionary.dao.impl.PtInfoDaoImpl.beanMap
java.lang.IllegalStateException: Failed to load ApplicationContext at org.springframework.test.conte ...