Use numpy array in shared memory for multiprocessing

后端 未结 5 1612
隐瞒了意图╮
隐瞒了意图╮ 2020-11-22 03:51

I would like to use a numpy array in shared memory for use with the multiprocessing module. The difficulty is using it like a numpy array, and not just as a ctypes array.

5条回答
  •  时光取名叫无心
    2020-11-22 04:12

    I've written a small python module that uses POSIX shared memory to share numpy arrays between python interpreters. Maybe you will find it handy.

    https://pypi.python.org/pypi/SharedArray

    Here's how it works:

    import numpy as np
    import SharedArray as sa
    
    # Create an array in shared memory
    a = sa.create("test1", 10)
    
    # Attach it as a different array. This can be done from another
    # python interpreter as long as it runs on the same computer.
    b = sa.attach("test1")
    
    # See how they are actually sharing the same memory block
    a[0] = 42
    print(b[0])
    
    # Destroying a does not affect b.
    del a
    print(b[0])
    
    # See how "test1" is still present in shared memory even though we
    # destroyed the array a.
    sa.list()
    
    # Now destroy the array "test1" from memory.
    sa.delete("test1")
    
    # The array b is not affected, but once you destroy it then the
    # data are lost.
    print(b[0])
    

提交回复
热议问题