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 作业,还是 pandas 有效利用所有内核而无需我明确告知? (在这种情况下,我将连续执行我的作业)。
我从阅读文档中可以看出,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,因此如果您不调用任何线程,您可能无法获得多线程的性能改进实际释放它的方法。
我正在使用 Pandas 0.18/Python 3.5 在英特尔 i3(四核)上编写代码。
我读过这个: https://www.continuum.io/content/pandas-releasing-gil
我还有一些 IO 绑定的工作(将 CSV 文件解析为数据帧)。 我必须做很多计算,主要是乘以数据帧。
我的代码目前使用concurrent.futures ThreadPoolExecutor
并行。
我的问题是:
- 一般来说,我应该使用线程来并行处理 运行 pandas 作业,还是 pandas 有效利用所有内核而无需我明确告知? (在这种情况下,我将连续执行我的作业)。
我从阅读文档中可以看出,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,因此如果您不调用任何线程,您可能无法获得多线程的性能改进实际释放它的方法。