I know this issue has been discussed here before, but I just cannot find any method that works. I want to share a global variable between my multiprocessing processes without any of the processes changing it, i.e. they just need read access. As a simple example, take:
def f(x):
return x**GlobalVar
if __name__ == '__main__':
GlobalVar = 6
pool = multiprocessing.Pool()
res= pool.map(f,[1,2,3,4])
print(res)
Now this obviously doesn't work as GlobalVar will not be accessible by the processes. So for it to work I would gave to evaluate GlobalVar, or import the it from a file, in each separate process. As in my application GlobalVar is a very large array, this is extremely wasteful. How can I easily share this Global Variable between the processes while just storing one copy of it in memory? I want to reiterate that the processes only need to read this global variable without changing it.