我在 ARM Raspbian linux、Python 2.7.13 和 amd64 Gentoo linux、Python 2.7.14 上运行代码
我有一个函数
import threading
def r() :
s = 1
while True:
s = s + 1
然后我为这个函数创建线程
t = threading.Thread(target=r)
t.start()
然后在 htop 中我可以看到另一个进程产生了(有它自己的 PID)!和 processing.Thread 文档说:
CPython implementation detail: In CPython, due to the Global Interpreter Lock, only one thread can execute Python code at once (even though certain performance-oriented libraries might overcome this limitation).If you want your application to make better use of the computational resources of multi-core machines, you are advised to use multiprocessing.
为什么此行为与文档不同?
最佳答案
这实际上是操作系统的问题:进程和线程在 Linux 上并没有太大区别。 Htop 将列出所有单独的线程,就好像它们是进程一样。 See this Unix Stack Exchange question了解更多信息。
关于python - 为什么 Python 2.7 为 threading.Thread 创建进程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51116129/