# author:jum_
# Event Events Monitor red light stop Green Line thread flag bit
# event provides three main methods wait, clear, set
# clear: Set "Flag" to False
#set: Set "Flag" to True
# with: Wait to set the flag bit
Import Threading,time
event = Threading. Event () #实例化一个事件
Def lights ():
Conten = 0 #初始化一个变量
Event.set () #设置事件标志位
While True:
If Conten >5 and conten<10:
Event.clear () #清空标志位
Print ("Red light")
Elif Conten >10:
Event.set () #设置标志位
Conten = 0 #清零
Else
Print ("green light") #事件触发时为绿灯
Time.sleep (1)
Conten+=1
def Car (name):
While True:
If Event.isset (): #判断设置标志位
Print ("[%s]runing ....%name")
Time.sleep (1)
Else
Print ("[%s]stop ..."% name)
Event.wait () #等待标志位设定
LG = Threading. Thread (Target=lights,)
Lg.start ()
BYD = Threading. Thread (target=car,args= ("BYD",))
Byd.start ()
‘‘‘
I have written before, incredibly can not understand, on the importance of annotation
‘‘‘
py3_ Thread Traffic Lights