问题描述
这是一个例子
import multiprocessing
import numpy as np
def inner_function(idx,return_dic,N):
c = np.random.choice(N)
return_dic[idx] = c
if __name__ == '__main__':
manager = multiprocessing.Manager()
return_dic = manager.dict()
jobs = []
for index in range(10):
p = multiprocessing.Process(
target=inner_function,args=(index,10)
)
jobs.append(p)
p.start()
for proc in jobs:
proc.join()
print(return_dic)
结果将是相同的(例如,{0: 4,1: 4,2: 4,3: 4,4: 4,5: 4,6: 4,7: 4,8: 4,9 : 4})。如何用多处理器生成随机数?
编辑:问题已解决。如果我们想生成随机值,似乎 numpy 会以相同的方式对待多处理器。所以即使我们调用随机函数,我们也会得到相同的值。我们需要为每个处理器手动设置随机种子。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)