Reputation: 423
I am wanting to share multiple resources between multiple python scripts. Ideally what I am trying to do is run my main program. It contains some script wide variables. I then want to fork off the exact same program into a new shell and give it access to my script wide variables.
I am looking into multiprocessing but I am not sure if another way would be better such as using pipes?
Upvotes: 5
Views: 3011
Reputation: 106533
You can use Value
and Array
from multiprocessing
for shared memory across processes.
Example from multiprocessing
's documentation:
from multiprocessing import Process, Value, Array
def f(n, a):
n.value = 3.1415927
for i in range(len(a)):
a[i] = -a[i]
if __name__ == '__main__':
num = Value('d', 0.0)
arr = Array('i', range(10))
p = Process(target=f, args=(num, arr))
p.start()
p.join()
print(num.value)
print(arr[:])
will output:
3.1415927
[0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
Upvotes: 3