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

标签 python subprocess python-multiprocessing

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

最佳答案

如果主要工作是在使用 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 关联性。 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?

关于python - Python 的 subprocess 和 multiprocessing 包如何互操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32463721/

相关文章:

python - 替换 pandas 中输入错误的日期

python - 并行 python 迭代

Python 输入 : declare type of callable when give it instance method

python 3 项目结构(单机与单机)多个模块

python - Pandas 列随时间的变化

python - 如何在后台运行 Python 子进程管道然后将其终止

python - 从 python 执行 DevCon CMD 命令

python - 将列表写入 HDFS 上的文件

python - celery :不允许守护进程有 child

python - 在 python 中清空 multiprocessing.queue(-1) 的首选方法