Pandas、Concurrent.Futures 和 GIL

Pandas, Concurrent.Futures and the GIL

我正在使用 Pandas 0.18/Python 3.5 在英特尔 i3(四核)上编写代码。

我读过这个: https://www.continuum.io/content/pandas-releasing-gil

我还有一些 IO 绑定的工作(将 CSV 文件解析为数据帧)。 我必须做很多计算,主要是乘以数据帧。

我的代码目前使用concurrent.futures ThreadPoolExecutor并行。

我的问题是:

我从阅读文档中可以看出,pandas simply releases the GIL for certain operations:

We are releasing the global-interpreter-lock (GIL) on some cython operations. This will allow other threads to run simultaneously during computation, potentially allowing performance improvements from multi-threading. Notably groupby, nsmallest, value_counts and some indexing operations benefit from this.

所有这意味着其他线程可以由 Python 解释器执行,而 pandas 继续进行计算。这并不意味着 pandas 会自动缩放跨多个线程的计算。他们也在文档中提到了这一点:

Releasing of the GIL could benefit an application that uses threads for user interactions (e.g. QT), or performing multi-threaded computations.

为了获得并行化优势,您需要在自己的代码中实际创建和执行多个线程。因此,如果您要在应用程序中尝试并行执行,则应继续使用 ThreadPoolExecutor

请记住,pandas 仅为 一些 操作释放 GIL,因此如果您不调用任何线程,您可能无法获得多线程的性能改进实际释放它的方法。