Python Multiprocessing/threading Code Exits Early
Solution 1:
Resolution: the new bug was closed as a duplicate of http://bugs.python.org/issue18966
Alas, there's no simple, satisfying explanation "for why". The cause is that multiprocessing
arranges for worker processes to leave Python via calling os._exit()
rather than the normal sys.exit()
. os._exit()
skips all "normal" shutdown processing. Part of what's skipped is .join()
-ing non-daemon threads, so the process just vanishes while the threads are still running.
That should at least (according to me) be documented, or preferably changed.
In the meantime, the workaround - as you already know - is to explicitly .join()
the threads yourself.
ANOTHER WAY
Under Python 3.4 or later, you could also use multiprocessing
's spawn
start method:
https://docs.python.org/3/library/multiprocessing.html?highlight=spawn#contexts-and-start-methods
That causes worker processes to finish via sys.exit(exitcode)
, which does all the normal shutdown processing (including .join()
-ing non-daemon threads).
spawn
is the only start method available on Windows, which explains why I had no problem running your original example.
Post a Comment for "Python Multiprocessing/threading Code Exits Early"