- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
python中的thread.start_new_thread
和threading.Thread.start
有什么区别?
我注意到,当调用 start_new_thread
时,新线程会在调用线程终止后立即终止。 threading.Thread.start
则相反:调用线程等待其他线程终止。
最佳答案
thread
模块是 Python 的低级线程 API。除非您确实需要,否则不建议直接使用它。 threading
模块是一个高级 API,构建在 thread
之上。 Thread.start
方法实际上是使用thread.start_new_thread
实现的。
Thread
的daemon
属性必须在调用start
之前设置,指定线程是否应该是守护进程。当没有留下任何事件的非守护线程时,整个 Python 程序就会退出。默认情况下,daemon
为 False
,因此该线程不是守护进程,因此该进程将等待其所有非守护线程退出,这是您的行为正在观察。
附言start_new_thread
真的很底层。它只是 Python 核心线程启动器的一个薄包装,它本身调用操作系统线程生成函数。
关于python - thread.start_new_thread 与 threading.Thread.start,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5882362/
当我单独运行下面的函数时,它会在 3 秒后退出,但是当我在线程中调用它时,它永远不会退出。请提出这段代码中的错误。 def display(val1, val2): root = Tk()
fdict= {0: fun1(), 1: fun2()} # approach 1 : working fine, printing string print fdict[random.randi
使用线程库时,有没有办法加入由 start_new_threads 创建的所有线程? 例如: try: import thread except ImportError: imp
我在 https://code.google.com/p/pyloadtools/wiki/CodeTutorialMultiThreading 找到了这个简单的代码 import _thread d
尝试在 Python 中使用 thread 时,使用以下代码 import select, sys, time, thread TIMEOUT = 30 def listenthread(server
当我使用旧的 Python thread API 时一切正常: thread.start_new_thread(main_func, args, kwargs) 但如果我尝试使用新的 threadin
python中的thread.start_new_thread和threading.Thread.start有什么区别? 我注意到,当调用 start_new_thread 时,新线程会在调用线程终止
我正在学习有关简单线程的教程。他们给出了这个例子,当我尝试使用它时,我从解释器那里得到了无法理解的错误。你能告诉我为什么这不起作用吗?我在 WinXP SP3 w/Python 2.6 current
我是一名优秀的程序员,十分优秀!