What happens to other threads when main thread calls sys.exit()? What happens to other threads when main thread calls sys.exit()? multithreading multithreading

What happens to other threads when main thread calls sys.exit()?


The documentation you reference comes from two different modules: thread and threading. thread is a low-level module providing more-or-less direct access to the platform's idea of what "thread" means. threading supplies a higher-level notion of "thread" with less platform dependence.

That's why the docs say different things. What happens to a low-level thread "thread" at exit does depend on what the platform C's version of threads do, but in any case Python makes no attempt to - or not to - shut them down cleanly.

A threading.Thread is different. Part of Python's normal shutdown processing is to .join() all non-daemon threading.Thread threads. So the program won't end at all until all non-daemon threading.Thread threads have ended (which is the programmer's responsibility to ensure). Note that the low-level thread module threads have no concept of .join() - .join() is a higher-level concept implemented by the distinct threading module.

Advice: use threading instead of thread unless you have excellent reasons to use thread instead. threading is better behaved and supplies many useful tools. An example of when using thread is better? I can't think of one ;-)

Note: in Python 3, the low-level thread module is renamed to _thread. As usual, the leading underscore hints "better not to mess with this - but it's here if you must".