我的程序里有这段代码。其中OnDone函数是wxPython GUI中的事件。当我单击按钮DONE时,OnDone事件会触发,然后执行一些功能,并启动线程self.tstart-with target function 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
我以前没有犯过这种错误。你们谁能告诉我这里缺了什么吗。
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
我想在“if”条件执行后加入线程。在他们来之前我想让这根线继续。
加入正在等待
连接一个线程实际上意味着等待另一个线程完成。
因此,在
thread1
中,可以有这样的代码:这意味着在这里停止,直到
thread2
完成“才执行下一行代码。如果您(在
thread1
中)执行了以下操作,则会失败,并出现问题中的错误:加入没有停止
调用
thread2.join()
不会导致thread2
停止,甚至不会以任何方式向它发出应该停止的信号。线程在其目标函数退出时停止。通常,一个线程被实现为一个循环,它检查一个告诉它停止的信号(变量),例如
然后,停止线程的方法是:
问题的代码
该代码已经用
break
正确地实现了停止。应该删除join
。我有个坏消息。Python中的线程是没有意义的,您最好只使用一个线程或使用多进程。如果您需要查看线程,则需要查看其他语言,如C或C
python中线程没有意义的原因是全局解释器锁(GIL)。这使得您一次只能使用一个线程,因此在python中没有多线程,但是有人在处理它。http://pypy.org/
当
StartEnable
方法执行时,它在您在__init__
方法中创建的StartEnablingThread上运行。不能加入当前线程。这在join调用的文档中有明确说明。我有个坏消息。Python中的线程是没有意义的,最好只使用一个线程或使用多个进程。如果您需要查看线程,则需要查看其他语言,如C或C
python中线程没有意义的原因是全局解释器锁(GIL)。这使得您一次只能使用一个线程,因此在python中没有多线程,但是有人在处理它。http://pypy.org/
相关问题 更多 >
编程相关推荐