运行Python多进程时性能逐渐降低

问题描述 投票:0回答:1

我正在使用multiprocess在多个情况下运行复杂的python方法(单次运行大约需要3-5分钟)。我注意到,当我第一次启动该程序时,运行速度是正常的:我可以在30分钟内使用30个内核在5分钟内获得大约20-30个输出,但是随着时间的流逝,性能逐渐降低,例如,在某个时候,我在30分钟内只能得到5个输出。然后我杀死程序并重新运行它,然后它遵循相同的行为。可能是什么原因?是因为开销吗?我能做什么?请参阅以下有关并行程序的示例代码:我正在代码中读取泡菜(类实例)

multiprocess

而且,当我打印出进程ID时,它在改变超时时间。这是预期的吗(使用新的进程ID启动了file_8)?输出类似于(如果使用5核):

import multiprocess as mp
import os
import pickle

def run_all_cases(input_folder):
    pickle_files = [x for x in os.listdir(input_folder)]
    jobs = [(file, input_folder) for file in pickle_files]
    num_process = max(mp.cpu_count()-1, 1)
    with mp.Pool(processes=num_process) as pool:
        pool.starmap(run_single_case, jobs)

def run_single_case(file_name, input_folder):
    print(f"started {file} using {os.getpid()}")
    data = pickle.load(input_folder + file_name)
    # a complicated method in a class 
    data.run_some_method()
    pickle.dump(data, f"{file_name.split("_")[0]}_output.pkl")
    print(f"finished {file} using {os.getpid()}")


=================================>

更新:我深入研究了某些实例,这些实例在处理完之后就死了。当我运行单个实例后,在一段时间后,在终端上显示:

started file_1 using core 8001
started file_2 using core 8002
started file_3 using core 8003
started file_4 using core 8004
started file_5 using core 8005
finished file_1 using core 8001
started file_6 using core 8001
finished file_2 using core 8002
started file_7 using core 8002
started file_8 using core 8006 #<-- it starts a new process id, rather than using the existing ones, is this expected?
finished file_3 using core 8003
...

我想这是问题所在:由于可能的内存问题(任何其他原因),进程被杀死,并且父进程没有启动新进程来继续运行作业,因此进程数随时间减少,因此我发现整体效果有所下降。

我正在使用多进程在多个情况下运行复杂的python方法(单次运行大约需要3-5分钟)。我注意到,当我第一次启动该程序时,运行速度是正常的:我可以...

python multiprocess
1个回答
0
投票

尝试杀死不再使用的旧物品。它获得了新的进程ID,因为多处理启动了新进程,而这些新进程将获得自己的进程ID。在其运行期间,查看系统上所有正在运行的进程,并查看正在运行的python程序实例的数量与预期的数量。

© www.soinside.com 2019 - 2024. All rights reserved.