Jes*_*dem 2 python multithreading wxpython
我的程序中有这段代码.其中OnDone函数是wxPython GUI中的事件.当我单击按钮DONE时,OnDone事件会启动,然后执行一些功能并启动线程self.tstart - 使用目标函数StartEnable.我希望使用self.tStart.join()加入这个线程.但是我收到如下错误:
Exception in thread StartEnablingThread:
Traceback (most recent call last):
File "C:\Python27\lib\threading.py", line 801, in __bootstrap_inner
self.run()
File "C:\Python27\lib\threading.py", line 754, in run
self.__target(*self.__args, **self.__kwargs)
File "//wagnernt.wagnerspraytech.com/users$/kundemj/windows/my documents/Production GUI/Trial python Codes/GUI_withClass.py", line 638, in StartEnable
self.tStart.join()
File "C:\Python27\lib\threading.py", line 931, in join
raise RuntimeError("cannot join current thread")
RuntimeError: cannot join current thread
Run Code Online (Sandbox Code Playgroud)
我之前没有遇到过这种类型的错误.你们其中任何一个人都可以告诉我这里缺少什么.
def OnDone(self, event):
self.WriteToController([0x04],'GuiMsgIn')
self.status_text.SetLabel('PRESSURE CALIBRATION DONE \n DUMP PRESSURE')
self.led1.SetBackgroundColour('GREY')
self.add_pressure.Disable()
self.tStart = threading.Thread(target=self.StartEnable, name = "StartEnablingThread", args=())
self.tStart.start()
def StartEnable(self):
while True:
time.sleep(0.5)
if int(self.pressure_text_control.GetValue()) < 50:
print "HELLO"
self.start.Enable()
self.tStart.join()
print "hello2"
break
Run Code Online (Sandbox Code Playgroud)
我想在"if"条件执行后加入线程.直到他们我希望线程运行.
加入一个线程实际上意味着等待另一个线程完成.
所以,在thread1,可以有代码说:
thread2.join()
Run Code Online (Sandbox Code Playgroud)
这意味着"停在这里,直到thread2完成后才执行下一行代码".
如果您执行thread1了以下操作,则会因问题中的错误而失败:
thread1.join() # RuntimeError: cannot join current thread
Run Code Online (Sandbox Code Playgroud)
呼叫thread2.join()不会导致thread2停止,甚至不以任何方式发出信号它应该停止.
当目标函数退出时,线程停止.通常,线程被实现为循环,其检查告知其停止的信号(变量),例如
def run():
while whatever:
# ...
if self.should_abort_immediately:
print 'aborting'
return
Run Code Online (Sandbox Code Playgroud)
然后,停止线程的方法是:
thread2.should_abort_immediately = True # tell the thread to stop
thread2.join() # entirely optional: wait until it stops
Run Code Online (Sandbox Code Playgroud)
该代码已经实现了正确停止break.本join应该只是被删除.
if int(self.pressure_text_control.GetValue()) < 50:
print "HELLO"
self.start.Enable()
print "hello2"
break
Run Code Online (Sandbox Code Playgroud)