Sharing data structures between two Python processes

I have 2 applications / process / scripts running with Python 3: is there some simple RPC mechanism for exchanging lists, tuples and data structures between these two separate processes?

To be precise, these 2 processes work locally, on the same machine, but a general solution that can also work with remote processes will be widely appreciated.

+4
source share
2 answers

If you start processes using multiprocessing, you can share the Array and Value variables at the process boundary.

Refuse this page of python document when using shared memory and multiprocessing

...

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

...

+5
source

Here the value may be

 'c': ctypes.c_char, 'u': ctypes.c_wchar, 'b': ctypes.c_byte, 'B': ctypes.c_ubyte, 'h': ctypes.c_short, 'H': ctypes.c_ushort, 'i': ctypes.c_int, 'I': ctypes.c_uint, 'l': ctypes.c_long, 'L': ctypes.c_ulong, 'f': ctypes.c_float, 'd': ctypes.c_double 

and therefore, initialization will correspond to it.

but params num and args are required.

+2
source

All Articles