Python 的 subprocess 和 multiprocessing 包如何互操作?

How does Python's subprocess and multiprocessing packages interoperate?

我想使用多处理在系统的多个核心上分散工作。作为工作的一部分,他们将运行subprocess.call(..., shell=True)。当他们这样做时会发生什么?子进程 fork 是否保留在该核心上?

如果主要工作在使用 subprocess 模块创建的子进程中完成,那么您不需要 multiprocessing 将工作分散到多个 CPU 核心。参见 Python threading multiple bash subprocesses?

What happens when they do that?

subprocess.call() 运行外部命令并等待它完成。它是否在 multiprocessing 模块创建的工作进程中启动并不重要。

Does the subprocess fork stay on that core?

如果你需要;您应该明确设置 CPU affinity。 psutil provides a portable way to set/get CPU affinity for a process.

如果您使用 numpy 那么它可能会影响 cpu 亲和力。参见 Why does multiprocessing use only a single core after I import numpy?