简体   繁体   English

使用 multiprocessing.Process 在 Python 中并行运行 function

[英]Running a function in Python in Parallel using multiprocessing.Process

I have a python function我有一个 python function

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

that takes the arguments需要 arguments

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

My laptop has 4 cores so as I understand it, the function should be able to be run 4 times at once with different arguments.我的笔记本电脑有 4 个内核,据我了解,function 应该能够使用不同的 arguments 一次运行 4 次。 The function doesn't currently return anything but just makes changes to the arguments. function 目前不返回任何内容,只是对 arguments 进行更改。

I have tried to run some code using the mp.Process module but this doesn't work (code below):我尝试使用 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() 

I have also looked into the Pool and starmap modules but have not been able to get these to work either.我还研究了 Pool 和 starmap 模块,但也无法让它们工作。 I think the problem has something to do with "pickle" but I'm not really sure what this is as I'm new to python as well as parallel processing!我认为这个问题与“泡菜”有关,但我不确定这是什么,因为我是 python 以及并行处理的新手!

Any help would be much appreciated!任何帮助将非常感激!

According to multiprocessing doc , it seems that args should be tuple.根据multiprocessing doc ,似乎args应该是元组。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM