简体   繁体   中英

thread.start_new_thread vs threading.Thread.start

What is the difference between thread.start_new_thread and threading.Thread.start in python?
I have noticed that when start_new_thread is called, the new thread terminates as soon as the calling thread terminates. threading.Thread.start is the opposite: the calling thread waits for other threads to terminate.

The thread module is the low-level threading API of Python. Its direct usage isn't recommended, unless you really need to. The threading module is a high-level API, built on top of thread . The Thread.start method is actually implemented using thread.start_new_thread .

The daemon attribute of Thread must be set before calling start , specifying whether the thread should be a daemon. The entire Python program exits when no alive non-daemon threads are left. By default, daemon is False , so the thread is not a daemon, and hence the process will wait for all its non-daemon thread to exit, which is the behavior you're observing.


PS start_new_thread really is very low-level. It's just a thin wrapper around the Python core thread launcher, which itself calls the OS thread spawning function.

See the threading.Thread.daemon flag - basically whenever no non-daemon threads are running, the interpreter terminates.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM