簡體   English   中英

python進程之間的共享內存

[英]Shared memory between python processes

我試圖找出一種在 python 進程之間共享內存的方法。 基本上存在多個python進程需要能夠讀取(僅讀取)和使用(無突變)的對象。 現在這是使用 redis + strings + cPickle 實現的,但是 cPickle 占用了寶貴的 CPU 時間,所以我不想使用它。 我在互聯網上看到的大多數 python 共享內存實現似乎都需要文件和泡菜,這基本上是我已經在做的,也正是我試圖避免的。

我想知道的是,是否有一種方法可以編寫類似……基本上是內存中的 Python 對象數據庫/服務器和相應的 C 模塊來與數據庫交互?

基本上,C 模塊會要求服務器提供一個地址來寫入對象,服務器會用一個地址響應,然后模塊會寫入對象,並通知服務器具有給定鍵的對象已寫入磁盤指定位置。 然后,當任何進程想要檢索具有給定鍵的對象時,他們只需向數據庫詢問給定鍵的內存位置,服務器將響應該位置,模塊將知道如何在內存中加載該空間和將python對象傳輸回python進程。

這是完全不合理的還是真的很難實施? 我在追求不可能的事情嗎? 歡迎大家提出意見。 謝謝互聯網。

不無道理。

IPC 可以通過內存映射文件來完成。 Python 具有內置功能:

http://docs.python.org/library/mmap.html

只需在兩個進程中映射文件,嘿,你就有了一個共享文件。 當然,您必須在兩個進程中輪詢它以查看發生了什么變化。 而且您必須在兩者之間合作寫入。 並決定您希望將數據放入哪種格式。但這是解決您問題的常用方法。

如果您不想酸洗, multiprocessing.sharedctypes可能適合。 不過,它有點低級; 您將獲得指定類型的單個值或數組。

將數據分發到子進程的另一種方式(一種方式)是multiprocessing.Pipe 它可以處理 Python 對象,而且它是用 C 實現的,所以我不能告訴你它是否使用了酸洗。

從 Python 3.8 開始,您可以使用multiprocessing.shared_memory.SharedMemory

Python 不支持獨立進程之間的共享內存。 你可以實現自己的C語言,或使用SharedArray如果您正在使用LIBSVM,numpy.ndarray,scipy.sparse工作。

# this a is clip from my project, can not run independently
def loadSvmShared (path, shape1, dtype=np.float32):
    dtype_size = dtype(1).itemsize
    indptr_size = np.int32(1).itemsize

    shm_name_prefix = 'shm://' + re.sub(r'[./]', '_', path) + '_'
    try:
        if conf.shm_force_clean:
            raise
        x_data    = SharedArray.attach(shm_name_prefix + 'x_data')
        x_indices = SharedArray.attach(shm_name_prefix + 'x_indices')
        x_indptr  = SharedArray.attach(shm_name_prefix + 'x_indptr')
        y1       = SharedArray.attach(shm_name_prefix + 'y')
        x1 = csr_matrix((x_data, x_indices, x_indptr), shape=(x_indptr.size - 1, shape1), copy=False)
        print('[done] reuse shared memory:', path)
    except Exception as err:
        unlinkShm(shm_name_prefix + 'x_data')
        unlinkShm(shm_name_prefix + 'x_indices')
        unlinkShm(shm_name_prefix + 'x_indptr')
        unlinkShm(shm_name_prefix + 'y')
        print('load from file:', path)
        d = load_svmlight_file(path, dtype=dtype, n_features=shape1)
        x: csr_matrix = d[0]
        y = d[1].astype(dtype).reshape((-1, 1))
        print('crate shared memory')
        x_data    = SharedArray.create(shm_name_prefix + 'x_data'   , x.data.size   , dtype=dtype   )
        x_indices = SharedArray.create(shm_name_prefix + 'x_indices', x.indices.size, dtype=np.int32)
        x_indptr  = SharedArray.create(shm_name_prefix + 'x_indptr' , x.indptr.size , dtype=np.int32)
        y1        = SharedArray.create(shm_name_prefix + 'y'        , y.shape       , dtype=dtype   )

        print('copy to shared memory')
        x_data[:] = x.data[:]
        x_indices[:] = x.indices[:]
        x_indptr[:] = x.indptr[:]
        x1 = csr_matrix((x_data, x_indices, x_indptr), shape=(x_indptr.size - 1, shape1), copy=False)
        y1[:] = y[:]
        print('[done] loaded data to shared memory:', path)
    print('x.shape:', x1.shape)
    print('y.shape:', y1.shape)
    return x1, y1

SharedArray: https://github.com/ddboline/shared-array : https://github.com/ddboline/shared-array

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM