当我在 56 核机器上使用进程池执行器触发它的并行实例时,我观察到 python 脚本的执行时间增加了。脚本 abc.py 导入一个沉重的 python 库,大约需要 1 秒。
time python ~/abc.py
real 0m0.846s
user 0m0.620s
sys 0m0.078s
测试方法
import shlex
from subprocess import Popen, PIPE
def test():
command = "python /u/deeparora/abc.py"
p = Popen(shlex.split(command), stdout=PIPE, stderr=PIPE)
p.wait(timeout=None)
下面的代码也需要 1 秒,这是预期的
串行执行
import concurrent.futures
pool = ProcessPoolExecutor(max_workers=1)
futures = []
for index in range(0, 1):
futures.append(pool.submit(test))
for future in concurrent.futures.as_completed(futures):
pass
但是下面的代码在 56 核机器上执行需要 5 秒
并行执行
import concurrent.futures
pool = ProcessPoolExecutor(max_workers=50)
futures = []
for index in range(0, 50):
futures.append(pool.submit(test))
for future in concurrent.futures.as_completed(futures):
pass
我检查了进程日志中的执行时间,可以注意到现在脚本(abc.py)的执行时间也从 1 秒增加到了 4 秒。有人可以帮我理解这种行为吗?