The global variable defined in main is accessed inside normal function but its not accessed in Pool()
from multiprocessing import Pool
import functools
def inc(x):
print( x + 1)
print(headers) # GETTING ERROR HERE
def dec(x):
print (x - 1)
def add(x, y):
print (x + y)
def a(f):
f()
def main():
print(headers)
f_inc = functools.partial(inc, 4)
f_dec = functools.partial(dec, 2)
f_add = functools.partial(add, 3, 4)
with Pool() as pool:
res = pool.map(a, [f_inc, f_dec, f_add])
print(res)
if __name__ == '__main__':
global headers
headers = {'Accept': 'application/json'}
main()
Expected output is
{'Accept': 'application/json'}
5
{'Accept': 'application/json'}
1
7
None
But the output i get is
{'Accept': 'application/json'}
5
NameError: name 'headers' is not defined
The global variable is not getting accessed inside pool in multiprocessing.