While writing and stress-testing a Python script which uses the multiprocessing library, I ran into the problem that occasionally the script hangs at the end. Literally runs past the last line of code and then hangs.
In this script I’m using Queues and Events, so I made sure that I properly close the queues in the forked/spawned (tried both) processes and also clean out all queues in the parent. Nevertheless occasionally - like seldom, but it happens - the script hangs. Checked the process list, checked remaining threads, checked the queues, all fine. Still …
When I hit Ctrl+C, I get the following stack trace:
|
|
(Can’t change the Python 3.9, as I don’t control the target system where this is supposed to run)
There’s nothing left to join, the only two remaining threads are MainThread
and QueueFeederThread
.
After some more debugging I had the simple idea not only to empty the queue before finishing the script, but setting the queue variable to None:
|
|
This helps. The script is no longer hanging at the end. At a later point I will see if the problem is fixed in newer versions.