empenguin
empenguin

Reputation: 323

Python multiprocessing cannot access variable defined in if __name__ == "main"

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]))

Upvotes: 1

Views: 952

Answers (1)

avigil
avigil

Reputation: 2246

Only the main thread has __name__=='__main__'. The other child threads import your code from a spawned process without setting __name__ to __main__. This is intentional, and required in windows (where fork() is not available), to provide a mechanism for executing code like initializing the pool only in the parent. See discussion here: Workaround for using __name__=='__main__' in Python multiprocessing

Upvotes: 2

Related Questions