Event: 是线程同步的一种方式,相似于一个标志,当该标志为false时,全部等待该标志的线程阻塞,当为true时,全部等待该标志的线程被唤醒spa
isSet(): 当内置标志为True时返回True。
set(): 将标志设为True,并通知全部处于等待阻塞状态的线程恢复运行状态。
clear(): 将标志设为False。
wait([timeout]): 若是标志为True将当即返回,不然阻塞线程至等待阻塞状态,等待其余线程调用set()线程
1 import threading 2 import time 3 4 event = threading.Event() 5 6 def func(): 7 print "%s is waiting for event...\n" % threading.currentThread().getName() 8 event.wait() 9 10 print "%s get the Event..\n" % threading.currentThread().getName() 11 12 13 if __name__ == "__main__": 14 t1 = threading.Thread(target=func) 15 t2 = threading.Thread(target=func) 16 17 t1.start() 18 t2.start() 19 20 time.sleep(2) 21 22 print "MainThread set Event\n" 23 24 event.set() 25 26 t1.join() 27 t2.join()