为什么Python I/O绑定的任务没有被GIL阻止? [英] Why is a Python I/O bound task not blocked by the GIL?

查看:102
本文介绍了为什么Python I/O绑定的任务没有被GIL阻止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

python 线程 文档指出"...线程仍然是合适的模型 如果您要同时运行多个受I/O约束的任务", 显然是因为受I/O约束的进程可以避免使用GIL来防止 来自CPU绑定任务中的并发执行的线程.

The python threading documentation states that "...threading is still an appropriate model if you want to run multiple I/O-bound tasks simultaneously", apparently because I/O-bound processes can avoid the GIL that prevents threads from concurrent execution in CPU-bound tasks.

但是我不明白的是,一个I/O任务仍在使用CPU.所以 它怎么可能不会遇到相同的问题?是因为I/O 绑定的任务将不需要内存管理吗?

But what I dont understand is that an I/O task still uses the CPU. So how could it not encounter the same issues? Is it because the I/O bound task will not require memory management?

推荐答案

CPython 1中的 GIL 仅与正在执行的Python代码有关.只要不需要与Python运行时进行交互,使用大量CPU的线程安全C扩展就可以释放GIL.

The GIL in CPython1 is only concerned with Python code being executed. A thread-safe C extension that uses a lot of CPU might release the GIL as long as it doesn't need to interact with the Python runtime.

一旦C代码需要与Python对话"(请参阅​​:回调到Python运行时),则它需要再次获取GIL-也就是说,GIL将为解释器"(并且我宽松地使用该术语),不是是为了防止本机/非Python代码同时运行.

As soon as the C code needs to 'talk' to Python (read: call back into the Python runtime) then it needs to acquire the GIL again - that is, the GIL is to establish protection/atomic behavior for the "interpreter" (and I use the term loosely) and is not to prevent native/non-Python code from running concurrently.

释放I/O周围的GIL(是否阻塞,是否使用CPU)是一回事-在将数据移入 Python之前,没有理由获取GIL.

Releasing the GIL around I/O (blocking or not, using CPU or not) is the same thing - until the data is moved into Python there is no reason to acquire the GIL.

1 GIL有争议,因为它在某些情况下阻止多线程CPython程序充分利用多处理器系统.请注意,潜在的阻塞或长时间运行的操作(例如I/O,图像处理和NumPy数字运算)发生在GIL之外.因此,只有在花费大量时间在GIL中并解释CPython字节码的多线程程序中,GIL才成为瓶颈.

1 The GIL is controversial because it prevents multithreaded CPython programs from taking full advantage of multiprocessor systems in certain situations. Note that potentially blocking or long-running operations, such as I/O, image processing, and NumPy number crunching, happen outside the GIL. Therefore it is only in multithreaded programs that spend a lot of time inside the GIL, interpreting CPython bytecode, that the GIL becomes a bottleneck.

这篇关于为什么Python I/O绑定的任务没有被GIL阻止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆