2013-04-02 98 views
4

我有2個使用Python 3運行的應用程序/進程/腳本:在這2個獨立進程之間共享列表,元組和數據結構有一些簡單的RPC機制?在2個Python進程之間共享數據結構

準確地說,這2個進程在本地工作,在同一臺機器上,但是一個通用的解決方案也可以在遠程進程中工作,我們將非常感謝。

回答

5

如果您啓動使用多進程,你能夠跨進程邊界共享ArrayValue變量。

檢查使用shared memorymultiprocessing

this python doc page ......

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

...

+0

謝謝,關於如何分享每個進程的正確ID的任何提示? – juio

2

這裏的值可以是

'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 

等初始化就會依據它。

但參數num和參數是強制性的。