For Python 2.7 you can try this:
for i in threading.enumerate(): if i.name == "MainThread": print i.is_alive()
The usage of lower camel case in function names is deprecated and so you should be using
i.is_alive()
instead ofi.isAlive()
.If you like one-liners try this:
is_main_thread_active = lambda : any((i.name == "MainThread") and i.is_alive() for i in threading.enumerate())
Then call
is_main_thread_active()
to check if the Main Thread is active.For one time use, however, you could use this directly without creating a function.
any((i.name == "MainThread") and i.is_alive() for i in threading.enumerate())
Check this page for more info about threading.
In python 3.4 a dedicated function to return the main thread exists and so you can use this one liner to see if your main thread is still alive..
threading.main_thread().is_alive()
Hope this helps you.