Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: How to detect when my thread become orphan?

I have a program using a thread. When my program is closed, my thread is still running and that's normal. I would like to know how my thread can detect that the main program is terminated; by itself ONLY. How would I do that?

My thread is in an infinite loop and process many object in a Queue. I can't define my thread as a daemon, else I can lose some data at the end of the main program. I don't want that my main program set a boolean value when it closed.

like image 736
user1528760 Avatar asked Jul 30 '12 16:07

user1528760


1 Answers

If you can get a handle to the main thread, you can call is_alive() on it.

Alternatively, you can call threading.enumerate() to get a list of all currently living threads, and check to see if the main thread is in there.

Or if even that is impossible, then you might be able to check to see if the child thread is the only remaining non-daemon thread.

like image 153
Taedrin Avatar answered Oct 02 '22 10:10

Taedrin