Python - 无法加入线程 - 没有多处理

时间:2016-10-06 18:16:32

标签: 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

之前我没有遇到过这种类型的错误。你们其中任何一个人都可以告诉我这里缺少什么。

    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”条件执行后加入线程。直到他们我希望线程运行。

3 个答案:

答案 0 :(得分:6)

加入等待

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

因此,在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

答案 1 :(得分:1)

Private Sub ClearAll_Click() Dim Sheet As Worksheet Dim CompareTool As Workbook Dim Sheetname As String Set CompareTool = ThisWorkbook With Application .DisplayAlerts = False .ScreenUpdating = True End With For Each Sheet In CompareTool.Worksheets If Left(Sheet.Name, 8) = "Scenario" Then Sheetname = Sheet.Name With CompareTool.Sheets(Sheetname) .Visible = True .Range("A1:EC168").Select .Visible = False End With End If Next Sheet Unload Me End Sub 方法正在执行时,它正在您使用StartEnable方法创建的 StartEnablingThread 上运行。您无法加入当前线程。 join电话的文档中明确说明了这一点。

  如果尝试加入当前线程,

join()会引发RuntimeError,因为这会导致死锁。在线程启动之前加入()线程也是错误的,并且尝试这样做会引发相同的异常。

答案 2 :(得分:-5)

我有一些坏消息。 Python中的线程是没有意义的,你最好只考虑使用1个线程或使用多个进程。如果您需要查看线程,那么您将需要查看其他语言,如C#或C.请查看https://docs.python.org/2/library/multiprocessing.html

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