为什么 numpy 计算不受全局解释器锁的影响?

Why are numpy calculations not affected by the global interpreter lock?

我正在决定是应该使用多处理还是多线程,并且我了解了一些关于 Global Interpreter Lock. In this nice blog post 的有趣信息,看来多线程不适合繁忙的任务。但是,我还了解到某些功能,例如 I/O 或 numpy,不受 GIL 的影响。

任何人都可以解释为什么,以及如何确定我的(可能相当笨重的)代码是否适合多线程?

许多 numpy计算不受GIL影响,但不是全部。

虽然在不需要 Python 解释器的代码中(例如 C 库),可以专门释放 GIL - 允许依赖于解释器的其他代码继续 运行。在 Numpy C 代码库中,宏 NPY_BEGIN_THREADSNPY_END_THREADS 用于分隔允许 GIL 释放的代码块。您可以在 this search of the numpy source.

中看到这些

NumPy C API documentation 有更多关于线程支持的信息。请注意额外的宏 NPY_BEGIN_THREADS_DESCRNPY_END_THREADS_DESCRNPY_BEGIN_THREADS_THRESHOLDED,它们处理有条件的 GIL 释放,具体取决于数组 dtypes 和循环的大小。

大多数核心功能都发布了 GIL - 例如 Universal Functions (ufunc) do so as described:

as long as no object arrays are involved, the Python Global Interpreter Lock (GIL) is released prior to calling the loops. It is re-acquired if necessary to handle error conditions.

关于你自己的代码,source code for NumPy is available。检查您为上述宏使用的函数(以及它们调用的函数)。另请注意,性能优势在很大程度上取决于 GIL 发布多长时间 - 如果您的代码不断下降 in/out of Python,您将看不到太多的改进。

另一种选择是测试它。但是,请记住,使用条件 GIL 宏的函数可能会对小型和大型数组表现出不同的行为。因此,使用小型数据集进行的测试可能无法准确表示大型任务的性能。

有一些关于使用 numpy 进行并行处理的附加信息 on the official wiki and a useful post about the Python GIL in general over on Programmers.SE