您的位置:首页 > 编程语言 > Python开发

python学习笔记--event事件

2017-12-02 19:54 381 查看
import threading,time

def light():
count = 0
while True:
if count < 10:
event.set()           #设置标志位
print("\033[42;1m ---green light is on \033[0m")
count += 1
elif count < 15:
event.clear()        #清除标志位
print("\033[41;1m ---red light is on \033[0m")
count += 1
else:
count  = 0
time.sleep(1)

def car(n):
while True:
if event.is_set():     #如果标志位设立了(即绿灯),那么车子就通行
print("car[ %s ] is running..." % n)
else:      #如果标志位没有设立(即红灯),那么车子就不通行
print("the car[ %s ] is waiting for red light..." % n )
time.sleep(2)

event = threading.Event()

t1 = threading.Thread(target=light)

t1.start()

for i in range(3):
t2 = threading.Thread(target=car,args=(i,))
t2.start()


—green light is on

car[ 0 ] is running…

car[ 1 ] is running…

car[ 2 ] is running…

—green light is on

—green light is on

car[ 1 ] is running…

car[ 2 ] is running…

car[ 0 ] is running…

—green light is on
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: