网站建设资讯

NEWS

网站建设资讯

event红绿灯实例

importthreading,time

event=threading.Event()#生成一个event对象

deflighter():
    count=0
    event.set()#开始设置标志位 绿灯 设置了标志位程序就通行
    while True:
        ifcount>5andcount<10:#改成红灯 6~9为红灯
            event.clear()#把标志位清了
            print("\033[41;1mred light is on ...\033[0m")
        elifcount>10:
            event.set()#变绿灯 设置标志位
            count =0#count清零 重新开始
        else:
            print("\033[42;1mgreen light is on ...\033[0m")#0~5位绿灯
        time.sleep(1)#每隔一秒记一次
        count +=1

defcar(name):
    while True:
        ifevent.is_set():#is_set 判断是否设置标志位 设置了标志位为绿灯 通行
            print("[%s] running..."%name)
            time.sleep(1)
        else:
            print("[%s] sees red light,waiting..."%name)
            event.wait()#wait卡住,等待标志位被设定 等到标志位设定之后程序再往下走
            print("\033[34;1m[%s] green light is on,start going...\033[34;0m"%name)


light=threading.Thread(target=lighter,)
light.start()

car1=threading.Thread(target=car,args=("Honda",))
car1.start()

创新互联建站是一家专注于成都网站设计、成都网站制作与策划设计,塔河网站建设哪家好?创新互联建站做网站,专注于网站建设十载,网设计领域的专业建站公司;建站业务涵盖:塔河等地区。塔河做网站价格咨询:028-86922220


新闻标题:event红绿灯实例
标题来源:http://cdweb.net/article/gijgis.html