python 线程even

import threading,time
import random
def door(nums):
    num=1#电梯在一楼
    while True:
        print("this door is %s 楼"%num)
        if num==nums:
            fangxiang=True
        elif num==1:
            fangxiang=False
        if fangxiang:
            print("this door is 下行")
            num=num-1
        else:
            print("this door is 上行")
            num=num+1
        #门只在偶数开门
        if num%2 and not event.isSet():
            event.set() #开门
            time.sleep(5)
        else:
            event.clear()
        time.sleep(2)
def man(n,doe):
    while True:
        time.sleep(random.randrange(10))
        if  event.isSet(): #门开啦
            print("man [%s] 在上电梯" % n)
        else:#门没开等点
            print("man [%s] 在等门开." % n)
if __name__=="__main__":
    event = threading.Event()
    Door = threading.Thread(target=door,args=(5,))
    Door.start()
    for i in range(3):
        t = threading.Thread(target=man,args=(i,5))
        t.start()

猜你喜欢

转载自www.cnblogs.com/leo0362/p/10917100.html
今日推荐