我在ARM Raspbian linux、Python2.7.13和amd64 Gentoo linux、Python2.7.14上运行代码
我有一个函数
import threading
def r() :
s = 1
while True:
s = s + 1
然后我为这个函数创建线程
t = threading.Thread(target=r)
t.start()
在htop中,我可以看到另一个进程生成(有自己的PID)!以及正在处理。线程文件上说:
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了解更多信息。你知道吗
相关问题 更多 >
编程相关推荐