gpt4 book ai didi

Python threading.thread.start() 不会将控制权返回给主线程

转载 作者:行者123 更新时间:2023-11-28 17:42:44 25 4
gpt4 key购买 nike

我正在尝试以这样一种方式执行一段代码的程序,即用户可以在不停止主程序的情况下随时停止其执行。我以为我可以使用 threading.Thread 来做到这一点,但后来我在 IDLE (Python 3.3) 中运行了以下代码:

from threading import *
import math
def f():
eval("math.factorial(1000000000)")
t = Thread(target = f)
t.start()

最后一行没有返回:我最终重新启动了 shell。这是全局解释器锁的结果,还是我做错了什么?我在线程文档 (http://docs.python.org/3/library/threading.html) 中没有看到任何关于此问题的具体信息

我尝试使用流程来做同样的事情:

from multiprocessing import *
import math
def f():
eval("math.factorial(1000000000)")

p = Process(target = f)
p.start()
p.is_alive()

最后一行返回 False,尽管我在启动该过程后仅运行了几秒钟!根据我的处理器使用情况,我不得不得出结论,这个过程从一开始就没有开始。有人可以解释一下我在这里做错了什么吗?

最佳答案

Thread.start() never returns! Could this have something to do with the C implementation of the math library?

作为@eryksun评论中指出:math.factorial() is implemented as a C function它不会释放 GIL,因此在它返回之前不会运行其他 Python 代码。

注意:multiprocessing 版本应该按原样工作:每个 Python 进程都有自己的 GIL。


factorial(1000000000) 有数亿位。尝试 导入时间; time.sleep(10) 作为虚拟计算。

如果您在 IDLE 中遇到多线程代码问题,请从命令行尝试相同的代码,以确保错误仍然存​​在。

如果 p.is_alive() 在调用 p.start() 后返回 False 那么这可能意味着有错误在 f() 函数中,例如 MemoryError

在我的机器上,p.is_alive() 返回 True,如果我将问题中的代码粘贴到 Python shell 中,其中一个 cpus 处于 100%。

不相关:删除通配符导入,例如 from multiprocessing import *。它们可能会掩盖您代码中的其他名称,因此您无法确定给定名称的含义,例如,threading 可以定义 eval 函数(它没有,但它可以) 具有相似但不同的语义,可能会悄无声息地破坏您的代码。

I want my program to be able to handle ridiculous inputs from the user gracefully

如果您将用户输入直接传递给 eval(),那么用户可以做任何事情

Is there any way to get a process to print, say, an error message without constructing a pipe or other similar structure?

这是一段普通的Python代码:

print(message) # works

不同之处在于,如果多个进程运行 print(),则输出可能会出现乱码。您可以使用锁来同步 print() 调用。

关于Python threading.thread.start() 不会将控制权返回给主线程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22138190/

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