thread.start_new_thread vs threading.Thread.start

陌路散爱 提交于 2019-12-01 16:18:56

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.


P.S. 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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!