What Am I Doing Wrong In This File With Pool.map Which Causes Nothing Appearing And I Have To Restart The Shell?
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
Solution 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
deff(x):
lo = 0for i inrange(x):
lo += i
return lo
deff_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
Solution 2:
You need to use the 'module name' idiom:
deff(x):
lo = 0for i inrange(x):
lo += i
return lo
from multiprocessing import Pool
deff_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.
Post a Comment for "What Am I Doing Wrong In This File With Pool.map Which Causes Nothing Appearing And I Have To Restart The Shell?"