首页 > 解决方案 > 在 python 中使用多处理运行多个 ML 模型

问题描述

我在 python 中使用 Multiprocessing 并行运行两个模型,代码如下:

def pro(process):      
    #print(process)                                                       
    os.system('python {}'.format(process)) 

def run_model_multiprocessing(ml_model1,ml_model2):

    processes = (ml_model1,ml_model2)   
    pool = Pool(processes=7)
    start = datetime.datetime.now()
    print('Start:',start)   
    pool.map(defs.pro, processes)
    end = datetime.datetime.now()
    print('End :',end)   
    total = end-start
    print('Total :', total)

但是我的模型都返回一个输出文件,但我无法在上述多处理过程中获取该输出文件。我已经使用下面的代码从两个模型中获取返回输出,但它对我不起作用。

def run_model_multiprocessing(ml_model1,ml_model2):     
    processes = (ml_model1,ml_model2)   
    pool = Pool(processes=7)
    start = datetime.datetime.now()
    print('Start:',start)   
    outdf1,outdf2 = pool.map(defs.pro, processes)
    end = datetime.datetime.now()
    print('End :',end)   
    total = end-start
    print('Total :', total)
    return outdf1, outdf2

虽然程序运行成功,但是outdf1and里面什么都没有outdf2

标签: pythonpython-3.xmultiprocessing

解决方案


如果我正确理解了您的查询,您的目标是并行运行两个机器学习模型 (ml_model1,ml_model2) 进行预测。

进程池仅提供数据并行性(通过并行处理数据的不同进程分发数据。)

如果您的目标是并行运行两个机器学习模型进行预测,那么您应该使用基于进程的并行性或基于线程的并行性。

请参考以下链接。

多处理与线程 Python


推荐阅读