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

标签 python python-multithreading

我在 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/

相关文章:

python - 使用 TensorFlow 对图像中的点进行插值采样

python - vagrant 和 ubunutu : pip install jupyter error: Storing debug log for failure in/home/vagrant/. pip/pip.log

python - 如何找到列表中不一定相邻的最大连续数字集?

python - 使用manager修改多进程进程之间的变量

python - 如何从 Flask Rest API 在后台运行长时间运行的 python 脚本?

python - 从特定索引开始的数据框 cummin 列

python - 由于numpy,pip安装apache-flink时出错

python - 使用并发.futures 上传 200 万个文件(每个文件大约 30 KB 从 EC2 到 S3),ThreadPool 需要大量时间

python - 当每个脚本在 python 中都有多个线程时,从主脚本同时运行两个脚本

Python Webdriver 多线程