daemon thread cleanup approach

Cameron Simpson cs at zip.com.au
Thu May 29 02:07:37 EDT 2014


On 28May2014 18:20, Carl Banks <pavlovevidence at gmail.com> wrote:
>Here's the solution I came up with: in the library's init function, it will start a non-daemon thread that simply joins the main thread, and then asks all existing worker threads to exit gracefully before timing out and leaving them to be killed.  So if an exception ends the main thread, there is still a chance to clean up properly.
>
>Does anyone see a potential problem with this approach?  It it possible that this will cause the program to hang in any case?  We can assume that all calls to the library will occur from the main thread, or at least from the same thread.  (If that isn't the case, then the caller has taken responsibility to ensure the program doesn't hang.)

That sounds safe to me, unless any of the subthreads call some C-level library 
routine that hangs even in a daemon thread. Which I assume either isn't the 
case or isn't a bug addressable this way anyway.

That's probably the best you can do from the sound of it, given that you may 
not hang (for longer than your timeout choice) and the calls are all at the 
whim of an external caller.

BTW, what were your dislikes of the with statement?

Disclaimer: I'm not a Windows guy.

Cheers,
Cameron Simpson <cs at zip.com.au>



More information about the Python-list mailing list