条件同步和条件变量同步差不多意思,只是少了锁功能,因为条件同步设计于不访问共享资源的条件环境,event=threading.Event():条件环境对象,初始值为False.
event.isSet():  返回event的状态值
event.wait():  如果event.isSet()==False将阻塞线程
event.set():  设置event的状态值为True,所有阻塞池的线程激活进入就绪状态,等待操作系统调度
event.clear():  恢复event的状态值为False

 import time,threading

 class Boss(threading.Thread):
def run(self):
print('Boss:今晚加班到22:00')
event.isSet()or event.set()
time.sleep(5)
print('Boss:<22:00>可以下班了')
event.isSet()orevent.set() class Worker(threading.Thread):
def run(self):
event.wait()
print('Worker:命苦啊~')
time.sleep(0.25)
event.clear()
event.wait()
print('Worker:oh,yeah') if __name__ == '__main__':
event = threading.Event()
threads = []
for i in range(5):
threads.append(Worker())
threads.append(Boss())
for t in threads:
t.start()
for t in threads:
t.join()
 import threading,time
import random
def light():
if not event.isSet():
event.set() #wait就不阻塞 #绿灯状态
count = 0
while True:
if count < 10:
print('\033[42;1m--green light on---\033[0m')
elif count <13:
print('\033[43;1m--yellow light on---\033[0m')
elif count <20:
if event.isSet():
event.clear()
print('\033[41;1m--red light on---\033[0m')
else:
count = 0
event.set() #打开绿灯
time.sleep(1)
count +=1
def car(n):
while 1:
time.sleep(random.randrange(10))
if event.isSet(): #绿灯
print("car [%s] is running.." % n)
else:
print("car [%s] is waiting for the red light.." %n)
if __name__ == '__main__':
event = threading.Event()
Light = threading.Thread(target=light)
Light.start()
for i in range(3):
t = threading.Thread(target=car,args=(i,))
t.start()

来自:http://www.cnblogs.com/yuanchenqi/articles/5733873.html

040同步条件event的更多相关文章

  1. Python学习---同步条件event/队列queue1223

    写在前面: 在使用这些共享API的时候,我们要注意以下几点: 在UNIX平台上,当某个进程终结之后,该进程需要被其父进程调用wait,否则进程成为僵尸进程(Zombie).所以,有必要对每个Proce ...

  2. Day12- Python基础12 线程、GIL、Lock锁、RLock锁、Semaphore锁、同步条件event

    http://www.cnblogs.com/yuanchenqi/articles/6248025.html  博客地址 本节内容: 1:进程和线程的说明 2:线程的两种调用方式 3:threadi ...

  3. 同步对象(同步条件Event)

    event = threading.Event()   #创建同步对象 event.wait()     #同步对象等待状态 event.set() #同步对象设置Trueevent.clear()  ...

  4. 27 python 初学(信号量、条件变量、同步条件、队列)

    参考博客: www.cnblogs.com/yuanchenqi/articles/5733873.html  semaphore 信号量: condition 条件变量: event 同步条件:条件 ...

  5. Python 线程同步变量,同步条件,列队

    条件变量同步 有一类线程需要满足条件之后才能够继续执行,Python提供了threading.Condition 对象用于条件变量线程的支持,它除了能提供RLock()或Lock()的方法外,还提供了 ...

  6. 多线程模块的同步机制event对象

    多线程模块的同步机制event对象 线程的核心特征就是他们能够以非确定的方式(即何时开始执行,何时被打断,何时恢复完全由操作系统来调度管理,这是用户和程序员无法确定的)独立执行的,如果程序中有其他线程 ...

  7. 经典线程同步 事件Event

    阅读本篇之前推荐阅读以下姊妹篇: <秒杀多线程第四篇 一个经典的多线程同步问题> <秒杀多线程第五篇 经典线程同步关键段CS> 上一篇中使用关键段来解决经典的多线程同步互斥问题 ...

  8. 多线程面试题系列(6):经典线程同步 事件Event

    上一篇中使用关键段来解决经典的多线程同步互斥问题,由于关键段的"线程所有权"特性所以关键段只能用于线程的互斥而不能用于同步.本篇介绍用事件Event来尝试解决这个线程同步问题.首先 ...

  9. [转]同步对象Event的用法

    同步对象Event的用法  首先介绍CreateEvent是创建windows事件的意思,作用主要用在判断线程退出,线程锁定方面.  CreateEvent函数功能描述:创建或打开一个命名的或无名的事 ...

随机推荐

  1. Ubuntu16.04 静态IP配置

    Ubuntu16.04 静态IP配置 修改配置 登录系统后,编辑文件/etc/network/interfaces.原始的内容如下: # This file describes the network ...

  2. C++测验代码

    /* 返回字符串前n位和返回整数前n位 */ #include <iostream> unsigned long left(unsigned long num, int n); char ...

  3. C++(笔)001.

    1.编程范式 编程范式是指计算机编程的基本风格,C++可容纳多种程度范式,如面向对象编程.泛型编程及传统的过程式编程. 2.与C相比较 C++在C语言的基础上新加的特性如下: a.类和对象.继承 b. ...

  4. django-admin管理后台高级自定义

    django自带的admin后台管理系统,在很多网站中被称为django的杀手级的应用.那么django-admin的适用情形倒底有哪些呢,一般 来说对于大型的商业性的项目通常不用采用django-a ...

  5. 设计模式入门,观察者模式,c++代码实现

    // test02.cpp : Defines the entry point for the console application.////设计模式第2章 观察者模式#include " ...

  6. 用一个小例子来谈谈javascript的运行机制

    先上例子! <script type="text/javascript"> console.log('博'); setTimeout(function(){ conso ...

  7. ES6中的import()函数

    import(specifier) 上面代码中,import函数的参数specifier,指定所要加载的模块的位置.import命令能够接受什么参数,import()函数就能接受什么参数,两者区别主要 ...

  8. cocos-creator 脚本逻辑-1

    1.节点 编辑组件的脚本文件时.可以通过以下语句获得节点 this 就是当前组件 this.node 拿到组件依附的节点 This.node.parent 拿到组件依附的节点 的父节点 This.no ...

  9. js和jq获取宽度和高度

    Javascript: console.log(document.body.clientWidth); //网页可见区域宽(body) console.log(document.body.client ...

  10. Django—Cookie and Session

    一.Cookie Cookie,有时也用其复数形式 Cookies,指某些网站为了辨别用户身份.进行 session 跟踪而储存在用户本地终端上的数据(通常经过加密). 1. 应用 服务器可以利用Co ...