Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I'll answer your second question first because it is easier. Yes, returning from the run method will indeed stop the thread. A detailed explanation is <a href="http://docs.python.org/library/threading.html#thread-objects" rel="nofollow">threading: Thread Objects</a> doc.</p> <p>To stop a thread that is running before it's natural completion you have to get a little more creative. There is no direct kill method on a thread object. What you need to do is use a shared variable to define the state of the thread.</p> <pre><code>alive = True class MyThread(threading.Thread): def run(): while(alive): #do work here </code></pre> <p>In some other piece of code, when you detect a condition for stopping that thread, the other thread simply sets alive to False:</p> <pre><code>alive = False </code></pre> <p>This is a simple example, I'll leave it to you to scale to multiple threads. </p> <h2>DANGER</h2> <p>This example works because reading and setting a boolean variable are <em>atomic</em> actions in python because of the Global Interpreter Lock. <a href="http://effbot.org/zone/thread-synchronization.htm" rel="nofollow">Here</a> is an excellent tutorial for lower level python threading. You should stick to using the Queue object because that's exactly what it's for.</p> <p>If you do anything more than reading and setting simple variables from multiple threads you should use <a href="http://docs.python.org/library/threading.html#lock-objects" rel="nofollow">Locks</a> or alternatively <a href="http://docs.python.org/library/threading.html#rlock-objects" rel="nofollow">Reentrant Locks</a> depending on your design and needs. Even something as simple as a compare and swap without a lock can cause problems in your program that are very difficult to debug.</p> <p>Another piece of advice for python multithreading is to never do any significant work in the interpreter thread. It should setup and start all the other threads and then sleep or wait on a <a href="http://docs.python.org/library/threading.html#condition-objects" rel="nofollow">condition object</a> until the program exits. The reason for this is no other python thread can receive operating system <a href="http://docs.python.org/library/signal.html" rel="nofollow">signals</a>. This means that no other thread can deal with <code>Ctrl+C</code> aka <code>KeyboardInterrupt</code> exceptions. It can be a good practice to have the main thread handle the <code>KeyboardInterrupt</code> exception and then set all the alive variables to False so you can exit your program quickly. This is especially helpful while developing so you don't have to constantly kill things when you make a mistake.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload