Using python multiprocess outside the main script
Multiprocessing doesn't have to run within the __main__
block, __main__
block is only used if the file is ran via python filename.py
.
So if you did:
m1.py
:
from multiprocessing import Pool def f(x): return x^2def f2(): p = Pool(5) p.map(f, [1,2,3,4,5])
m2.py
:
from m1 import f2if __name__ == '__main__': f2() # this would run multiprocessing code
and then call python m2.py
, your code would run correctly, with mp.