首页 > 解决方案 > 使用 multiprocessing.Process 在 Python 中并行运行函数

问题描述

我有一个python函数

def noflux(j,i,cells,a,b,c,d):

接受论点

j,i (integers)
cells (shape = (87,72), Type: ndarray),
a,b,c,d (shape = (87,72), Type: ndarray)

我的笔记本电脑有 4 个内核,据我了解,该函数应该能够使用不同的参数一次运行 4 次。该函数当前不返回任何内容,只是对参数进行更改。

我尝试使用 mp.Process 模块运行一些代码,但这不起作用(下面的代码):

p1 = mp.Process(target=noflux,args=[j,i,cells,rfu,lfu,ufu,dfu])
p2 = mp.Process(target=noflux,args=[j,i,cells,rfh,lfh,ufh,dfh])

p1.start()
p2.start()
            
p1.join()
p2.join() 

我还研究了 Pool 和 starmap 模块,但也无法让它们工作。我认为这个问题与“pickle”有关,但我不确定这是什么,因为我是 python 以及并行处理的新手!

任何帮助将非常感激!

标签: pythonnumpymultiprocessingwindows-10python-multiprocessing

解决方案


根据multiprocessing doc,似乎args应该是元组。

p2 = mp.Process(target=noflux,args=(j,i,cells,rfh,lfh,ufh,dfh,))

推荐阅读