gpt4 book ai didi

python - thread.start_new_thread 与 threading.Thread.start

转载 作者:太空狗 更新时间:2023-10-29 20:50:36 27 4
gpt4 key购买 nike

python中的thread.start_new_threadthreading.Thread.start有什么区别?
我注意到,当调用 start_new_thread 时,新线程会在调用线程终止后立即终止。 threading.Thread.start 则相反:调用线程等待其他线程终止。

最佳答案

thread 模块是 Python 的低级线程 API。除非您确实需要,否则不建议直接使用它。 threading 模块是一个高级 API,构建在 thread 之上。 Thread.start方法实际上是使用thread.start_new_thread实现的。

Threaddaemon 属性必须在调用start 之前设置,指定线程是否应该是守护进程。当没有留下任何事件的非守护线程时,整个 Python 程序就会退出。默认情况下,daemonFalse,因此该线程不是守护进程,因此该进程将等待其所有非守护线程退出,这是您的行为正在观察。


附言start_new_thread 真的很底层。它只是 Python 核心线程启动器的一个薄包装,它本身调用操作系统线程生成函数。

关于python - thread.start_new_thread 与 threading.Thread.start,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5882362/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com