我有一个类似的代码:
import numpy as np
import multiprocessing as mp
a = np.zeros((4, 4)) # 4x4 array containing zeros
def f(x, y):
# uses scipy functions
# takes long to compute
#result = <someValue after calculation>
global a
a[x][y] = x+y # simple example function
# since f takes long to compute, I want to run it in parallel
jobs = []
for x in range(4):
for y in range(4):
p = mp.Process(target=f, args=(x, y))
p.start()
jobs.append(p)
# wait for a to be filled for the next step
for j in jobs:
j.join()
print a # prints an array of zeros!
为什么没有将值分配给 a ,我该如何解决这个问题?谢谢!