Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As a commenter already mentioned, you can't use <code>join()</code> because all that does is wait for the thread to terminate naturally. I'm not aware of any native Python API to forcibly kill a thread, although the underlying threading libraries will typically have one (e.g. <code>pthread_kill()</code>). This is generally because forcibly killing a thread in Python is generally a pretty bad thing to do.</p> <p>In this case, however, it doesn't appear you're trying to kill an uncooperative thread, you've signalled to the thread that you want it to terminate gracefully (by setting your <code>running</code> attribute).</p> <p>I don't see any reason why you can't just set <code>thread</code> to <code>None</code> straight away - I don't believe that removing the last reference to the thread object will actually cause any problems as the thread will be kept alive until it terminates (unless you've set <code>daemon</code> to <code>True</code> on it and the main program terminates). Of course, any objects that are in the closure created when you define the <code>worker()</code> function will still exist, so they won't be freed until the thread terminates (and possibly not even then the way that closure is set up - I'd need to think about that more carefully).</p> <p>Anyway, I think your life will be easier if you just arrange to <code>join()</code> with the thread when it terminates. If your concern is that you will have to wait for your thread's period to time out, you can use a <code>threading.Condition</code> object to get around that:</p> <pre><code>class CoolThing(object): def __init__(self): self.thread = None self.exit_condition = threading.Condition() def run_in_background(self, callback, period=0.5): if self.thread: raise RuntimeError def worker(exit_condition): exit_condition.acquire() worker.running = True while worker.running: if some_event(): callback(self) exit_condition.wait(period) exit_condition.release() self.thread = (threading.Thread(target=worker, args=(self.exit_condition,)), worker) self.thread[0].start() def stop_background(self): if not self.thread: raise RuntimeError # Make the worker function end harmfully. self.exit_condition.acquire() self.thread[1].running = False self.exit_condition.notify() self.exit_condition.release() self.thread[0].join() self.thread = None </code></pre>
    singulars
    1. This table or related slice is empty.
    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. This table or related slice is empty.
    1. This table or related slice is empty.
    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