简体   繁体   English

为什么GIL不阻止Python I / O绑定任务?

[英]Why is a Python I/O bound task not blocked by the GIL?

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. python线程文档指出“......如果你想同时运行多个I / O绑定任务,线程仍然是一个合适的模型”,显然是因为I / O绑定进程可以避免阻止线程并发执行的GIL CPU绑定任务。

But what I dont understand is that an I/O task still uses the CPU. 但我不明白的是I / O任务仍然使用CPU。 So how could it not encounter the same issues? 那怎么能不遇到同样的问题呢? Is it because the I/O bound task will not require memory management? 是因为I / O绑定任务不需要内存管理吗?

All of Python's blocking I/O primitives release the GIL while waiting for the I/O block to resolve -- it's as simple as that! 所有Python的阻塞I / O原语在等待I / O块解析时释放GIL - 就这么简单! They will of course need to acquire the GIL again before going on to execute further Python code, but for the long-in-terms-of-machine-cycles intervals in which they're just waiting for some I/O syscall, they don't need the GIL, so they don't hold on to it! 他们当然需要在继续执行更多Python代码之前再次获取GIL,但是对于他们只是等待一些I / O系统调用的长期机器周期间隔,他们不会不需要GIL,所以他们不坚持下去!

The GIL in CPython 1 is only concerned with Python code being executed. CPython 1中GIL 关注正在执行的Python代码。 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. 只要不需要与Python运行时交互,使用大量CPU的线程安全C扩展可能会释放GIL。

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. 一旦C代码需要与Python“交谈”(读取:回调到Python运行时),那么它需要再次获取GIL - 也就是说,GIL是为“解释器”建立保护/原子行为(我使用松散的术语, 并不是为了防止本机/非Python代码同时运行。

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. 周围释放I / O(阻塞或不使用CPU与否)的GIL是同样的事情-直到数据被移动的Python没有理由获取GIL。


1 The GIL is controversial because it prevents multithreaded CPython programs from taking full advantage of multiprocessor systems in certain situations. 1 GIL存在争议,因为它可以防止多线程CPython程序在某些情况下充分利用多处理器系统。 Note that potentially blocking or long-running operations, such as I/O, image processing, and NumPy number crunching, happen outside the GIL. 请注意,在GIL之外发生可能阻塞或长时间运行的操作,例如I / O,图像处理和NumPy数字运算。 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. 因此,只有多线程程序在GIL中花费大量时间来解释CPython字节码 ,GIL才成为瓶颈。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM