1 #!/usr/bin/python 2 #coding=utf-8 3 #用于线程间通信,通过事件标识控制 4 import threading 5 from time import sleep,ctime 6 7 def A(): 8 print "A is starting" 9 event_is_set=e.wait() 10 print "event set1:%s"%event_is_set 11 12 def B(e,t): 13 while not e.isSet(): 14 print "wait for event timeout starting" 15 event_is_set=e.wait(t) 16 print "event set2:%s"%event_is_set 17 if event_is_set: 18 print "event process" 19 else: 20 print "do other work" 21 22 e=threading.Event() 23 t1=threading.Thread(target=A) 24 t1.start() 25 t2=threading.Thread(target=B,args=(e,4)) 26 t2.start() 27 28 print "waiting before calling event set" 29 sleep(5) 30 e.set() 31 print "event is set"