为什么 Python 2.7 为 threading.Thread 创建进程
Why Python 2.7 creates processes for threading.Thread
I 运行 代码在 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.
为什么此行为与文档不同?
这实际上是 OS 的事情:进程和线程在 Linux 上没有太大区别。 Htop 将列出所有单独的线程,就好像它们是进程一样。 See this Unix Stack Exchange question 获取更多信息。
I 运行 代码在 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.
为什么此行为与文档不同?
这实际上是 OS 的事情:进程和线程在 Linux 上没有太大区别。 Htop 将列出所有单独的线程,就好像它们是进程一样。 See this Unix Stack Exchange question 获取更多信息。