Python-无法连接线程-无多处理

2024-04-23 12:26:26 发布

您现在位置:Python中文网/ 问答频道 /正文

我的程序里有这段代码。其中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”条件执行后加入线程。在他们来之前我想让这根线继续。


Tags: inpyselftargetlinegui线程thread
4条回答

加入正在等待

连接一个线程实际上意味着等待另一个线程完成。

因此,在thread1中,可以有这样的代码:

thread2.join()

这意味着在这里停止,直到thread2完成“才执行下一行代码。

如果您(在thread1中)执行了以下操作,则会失败,并出现问题中的错误:

thread1.join()    # RuntimeError: cannot join current thread

加入没有停止

调用thread2.join()不会导致thread2停止,甚至不会以任何方式向它发出应该停止的信号。

线程在其目标函数退出时停止。通常,一个线程被实现为一个循环,它检查一个告诉它停止的信号(变量),例如

def run():
    while whatever:
        # ...
        if self.should_abort_immediately:
            print 'aborting'
            return

然后,停止线程的方法是:

thread2.should_abort_immediately = True  # tell the thread to stop
thread2.join()  # entirely optional: wait until it stops

问题的代码

该代码已经用break正确地实现了停止。应该删除join

        if int(self.pressure_text_control.GetValue()) < 50:
            print "HELLO"
            self.start.Enable()
            print "hello2"
            break

我有个坏消息。Python中的线程是没有意义的,您最好只使用一个线程或使用多进程。如果您需要查看线程,则需要查看其他语言,如C或C

python中线程没有意义的原因是全局解释器锁(GIL)。这使得您一次只能使用一个线程,因此在python中没有多线程,但是有人在处理它。http://pypy.org/

StartEnable方法执行时,它在您在__init__方法中创建的StartEnablingThread上运行。不能加入当前线程。这在join调用的文档中有明确说明。

join() raises a RuntimeError if an attempt is made to join the current thread as that would cause a deadlock. It is also an error to join() a thread before it has been started and attempts to do so raises the same exception.

我有个坏消息。Python中的线程是没有意义的,最好只使用一个线程或使用多个进程。如果您需要查看线程,则需要查看其他语言,如C或C

python中线程没有意义的原因是全局解释器锁(GIL)。这使得您一次只能使用一个线程,因此在python中没有多线程,但是有人在处理它。http://pypy.org/

相关问题 更多 >