python - 使用 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 以及并行处理的新手!
任何帮助将非常感激!
解决方案
根据multiprocessing doc,似乎args
应该是元组。
p2 = mp.Process(target=noflux,args=(j,i,cells,rfh,lfh,ufh,dfh,))
推荐阅读
- javascript - 如何用 Tampermonkey 替换弹出窗口的 HTML 代码?
- bash - 删除不必要的空行
- node.js - 是否可以在没有 sudo 访问的情况下设置 PM2 自动重启?
- c++ - string_view 实现使用指针,大小而不是指针,指针是否有原因
- amazon-web-services - 在启动新实例时通过 AWS 快照保留相同的 IP 和主机名
- javascript - websocketserver应用程序中的范围问题
- performance - 缓存未使用的节点可以加速链表吗?
- javascript - 从 AWS Lambda 提取检索旧数据
- javascript - 在表单按钮 onclick 后应用 Html 样式更改时不会坚持
- python-3.x - 在 Python 3 中经常调用的函数之外定义一个变量