Skip to main content
 首页 » 编程设计

python之为什么 Python 2.7 为 threading.Thread 创建进程

2025年05月04日57Free-Thinker

我在 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了解更多信息。