py3_ Thread Traffic Lights

Source: Internet
Author: User

# 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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.