Thread._stop() behavior changed in Python 3.4
solipsis at pitrou.net
Mon Mar 17 18:33:09 CET 2014
Felix Yan <felixonmars <at> gmail.com> writes:
> A minimized snippet to reproduce:
> import threading
> def stale():
> import time
> t = threading.Thread(target=stale)
> This works correctly with Python 3.3, the program exits immediately after
> t._stop() called, and no exception was raised.
Basically what you are doing is abusing a private method because you want
to make the thread daemonic after it was started (a daemonic thread is
not waited for at interpreter exit). Please do note one thing: the _stop()
method does *not* actually stop the thread; it just marks it stopped, but
the underlying OS thread continues to run (and may indeed continue to
execute Python code until the interpreter exits).
So the obvious "solution" here is to mark the thread daemonic before
A possible related improvement would be to relax the contraints on
Thread.daemon to allow setting the flag on a running thread?
That said, daemon threads (or abuse of the _stop() method as you did) can
lead to instabilities and oddities as some code will continue executing while
the interpreter starts shutting down. This has been improved but perhaps
not totally solved in recent interpreter versions. A fully correct solution
would involve gracefully telling the thread to shut down, via a boolean
flag, an Event, a file descriptor or any other means.
(if you are interested in this, please open a new issue at
More information about the Python-list