问题
What am I missing or doing wrong in the following python file using multiprocessing? When I run it, nothing happens and I have to restart the shell!
def f(x):
lo=0
for i in range(x):
lo+=i
return(lo)
from multiprocessing import Pool
def f_parallel(x1,x2,x3,x4):
with Pool(processes=4) as pool:
resulto_parallel=pool.map(f,[x1,x2,x3,x4])
return(resulto_parallel)
f_parallel(1,2,3,4)
Here is screenshot of what happens when I run it.
And then after waiting a while I just restart the shell.
回答1:
Quoting from the multiprocessing docs:
one should protect the “entry point” of the program by using
if __name__ == '__main__'
In your case this looks like:
from multiprocessing import Pool
def f(x):
lo = 0
for i in range(x):
lo += i
return lo
def f_parallel(x1, x2, x3, x4):
with Pool(processes=4) as pool:
resulto_parallel = pool.map(f, [x1, x2, x3, x4])
return resulto_parallel
if __name__ == '__main__':
print(f_parallel(1, 2, 3, 4))
The key point is that the spawned processes will import your python script, which, if you didn't protect the call to f_parallel(1,2,3,4)
would result in an endless spawning of subprocesses.
Note: Also added a print
so that the result of the function call will actually be put to stdout
回答2:
You need to use the 'module name' idiom:
def f(x):
lo = 0
for i in range(x):
lo += i
return lo
from multiprocessing import Pool
def f_parallel(x1, x2, x3, x4):
with Pool(processes=4) as pool:
return pool.map(f,[x1, x2, x3, x4])
if __name__ == '__main__':
print(f_parallel(1, 2, 3, 4))
Output:
[0, 1, 3, 6]
Without the if __name__ == '__main__':
part above, multiprocessing
re-reads the module (executing it) and gets into an infinite loop.
来源:https://stackoverflow.com/questions/52698370/what-am-i-doing-wrong-in-this-file-with-pool-map-which-causes-nothing-appearing