I use windows. I don't understand why the following code fails.
a
, when defined in the "if main" block, should be a global variable. But running the script I got error "a is not defined". However, if a
is defined outside the "if main" block, the code will work.
from multiprocessing import Pool
import numpy as np
# a = np.array([1,2,3])
def f(x):
return a*x
if __name__ == '__main__':
a = np.array([1,2,3])
with Pool(5) as p:
print(p.map(f, [1, 2, 3]))