The following code will complain NameError: name ‘b’ is not defined
from concurrent.futures import ProcessPoolExecutor
def add(a):
return a+b
if __name__ == '__main__':
b = 3
with ProcessPoolExecutor() as executor:
test = executor.submit(add,5)
print(test.result())
But if you move b = 3 before the if statement as shown below, it will run without error, why?
from concurrent.futures import ProcessPoolExecutor
def add(a):
return a+b
b = 3
if __name__ == '__main__':
with ProcessPoolExecutor() as executor:
test = executor.submit(add,5)
print(test.result())
And by the way, what's the best practice to define global variables like b in functions?