Python - Cannot join thread - No multiprocessing
<p>I have this piece of code in my program. Where OnDone function is an event in a wxPython GUI. When I click the button DONE, the OnDone event fires up, which then does some functionality and starts the thread self.tstart - with target function StartEnable. This thread I want to join back using self.tStart.join(). However I am getting an error as follows: </p>

<pre><code>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
</code></pre>

<p>I have not got this type of error before. Could any one of you guys tell me what I am missing here. </p>

<pre><code>    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()) &lt; 50:
                print "HELLO"
                self.start.Enable()
                self.tStart.join()
                print "hello2"
                break
</code></pre>

<p>I want to join the thread after the "if" condition has executed. Until them I want the thread to run. </p>

<h2>Joining Is Waiting</h2>

<p><em>Joining</em> a thread actually means waiting fo another thread to finish.</p>

<p>So, in <code>thread1</code>, there can be code which says:</p>

<pre><code>thread2.join()
</code></pre>

<p>That means <em>"stop here and do not execute the next line of code until <code>thread2</code> is finished"</em>.</p>

<p>If you did (in <code>thread1</code>) the following, that would fail with the error from the question:</p>

<pre><code>thread1.join()    # RuntimeError: cannot join current thread
</code></pre>

<h2>Joining Is Not Stopping</h2>

<p>Calling <code>thread2.join()</code> does not cause <code>thread2</code> to stop, nor even signal to it in any way that it should stop.</p>

<p>A thread stops when its target function exits. Often, a thread is implemented as a loop which checks for a signal (a variable) which tells it to stop, e.g.</p>

<pre><code>def run():
    while whatever:
        # ...
        if self.should_abort_immediately:
            print 'aborting'
            return
</code></pre>

<p>Then, the way to stop the thread is to do:</p>

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

<h1>The Code from the Question</h1>

<p>That code already implements the stopping correctly with the <code>break</code>. The <code>join</code> should just be deleted.</p>

<pre><code>        if int(self.pressure_text_control.GetValue()) &lt; 50:
            print "HELLO"
            self.start.Enable()
            print "hello2"
            break
</code></pre>

python multithreading wxpython