python线程的事件用于主线程控制其他线程的执行,事件主要提供了三个方法wait、clear、set
事件处理的机制:全局定义了一个“Flag”,如果“Flag”值为 False,那么当程序执行 event.wait 方法时就会阻塞,如果“Flag”值为True,那么event.wait 方法时便不再阻塞。
- clear:将“Flag”设置为False
- set:将“Flag”设置为True
python threading 模块提供了EVENT对象用于线程间的通信,它设置了 ,清除 ,等待 ,设置等方法用于线程之间的通信 .
event是线程之间最简单的通信方式之一,一个线程产生一个信号 ,另外一个线程则等待这个信号
python通过 threading.event()产生一个event 对象,event对象维护一个内部标志(标志的初始为false),通过set()将其设置为true
wait(timeout)则用于阻塞线程直至FLAG被set(或者超时 ,可选的)
isSet()用于查询标志位为TRUE
clear()用于清除标志位
1 设置信号
CMD_ADBINSTALL = "adb install " class topapkThread(threading.Thread): def __init__(self, filepath, listname, len, window): # 文件路径, 名称 ,index,self threading.Thread.__init__(self) self.listname = listname self.len = len self.window = window self.filepath = filepath self.timeToQuit = threading.Event() self.timeToQuit.clear() self.index = 0 def stop(self): print "stop thread" self.timeToQuit.set() def run(self): while (self.index < self.len) : cmd_shell = CMD_ADBINSTALL + self.filepath + "\\" + self.listname[self.index] b = os.system(cmd_shell) print b print "执行安装 " + str(self.listname[self.index]) if b == 0: print "安装成功" else: print "安装失败" # print "执行安装 " + str(self.listname[self.index]) self.index += 1 # 等待30S print "wait 30" self.timeToQuit.wait(30) #继续执行 print "go on" if self.timeToQuit.isSet(): print "手动停止线程运行" break else: print "执行完成---over " wx.CallAfter(self.window.ThreadFinished,self)