python - 使用 Python 共享内存的段错误

标签 python macos numpy shared-memory python-3.8

函数 store_in_shm 将一个 numpy 数组写入共享内存,而第二个函数 read_from_shm 使用同一共享内存空间中的数据创建一个 numpy 数组并返回 numpy 数组。
但是,在 Python 3.8 中运行代码会出现以下段错误:

zsh: segmentation fault python foo.py


为什么在函数 read_from_shm 内部访问 numpy 数组没有问题,但在函数外再次访问 numpy 数组时出现段错误?
输出:
From read_from_shm(): [0 1 2 3 4 5 6 7 8 9]
zsh: segmentation fault  python foo.py
% /Users/athena/opt/anaconda3/envs/test/lib/python3.8/multiprocessing/resource_tracker.py:203: UserWarning: resource_tracker: There appear to be 1 leaked shared_memory objects to clean up at shutdown
  warnings.warn('resource_tracker: There appear to be %d '
foo.py
import numpy as np
from multiprocessing import shared_memory

def store_in_shm(data):
    shm = shared_memory.SharedMemory(name='foo', create=True, size=data.nbytes)
    shmData = np.ndarray(data.shape, dtype=data.dtype, buffer=shm.buf)
    shmData[:] = data[:]
    shm.close()
    return shm

def read_from_shm(shape, dtype):
    shm = shared_memory.SharedMemory(name='foo', create=False)
    shmData = np.ndarray(shape, dtype, buffer=shm.buf)
    print('From read_from_shm():', shmData)
    return shmData

if __name__ == '__main__':
    data = np.arange(10)
    shm = store_in_shm(data)
    shmData = read_from_shm(data.shape, data.dtype)
    print('From __main__:', shmData)    # no seg fault if we comment this line
    shm.unlink()

最佳答案

基本上问题似乎是当函数返回时 shm 被垃圾收集时,底层的 mmap'ed 文件(由 read_from_shm 内的 shm 拥有)正在关闭。然后 shmData 指回它,这是您获得段错误的地方(用于指代关闭的 mmap)这似乎是 known bug ,但可以通过保留对 shm 的引用来解决。
此外,所有 SharedMemory 实例都希望被 close() 'd,当不再需要时,其中一个被 unlink() 'ed。如果您自己不调用 shm.close(),它将在 GC 中被调用,并且在 Windows 上,如果它是当前“打开”的唯一一个,则共享内存文件将被删除。当您在 shm.close() 中调用 store_in_shm 时,您会引入操作系统依赖项,因为在 Windows 上数据将被删除,而 MacOS 和 Linux 将保留直到 unlink 被调用。
最后,虽然这不会出现在您的代码中,但 another problem 当前存在,从独立进程(而不是子进程)访问数据可以类似地过早删除底层 mmap。 SharedMemory 是一个非常新的库,希望所有问题都能很快解决。
您可以重新编写给定的示例以保留对“第二个” shm 的引用,并且只使用其中一个到 unlink :

import numpy as np
from multiprocessing import shared_memory

def store_in_shm(data):
    shm = shared_memory.SharedMemory(name='foo', create=True, size=data.nbytes)
    shmData = np.ndarray(data.shape, dtype=data.dtype, buffer=shm.buf)
    shmData[:] = data[:]
    #there must always be at least one `SharedMemory` object open for it to not
    #  be destroyed on Windows, so we won't `shm.close()` inside the function,
    #  but rather after we're done with everything.
    return shm

def read_from_shm(shape, dtype):
    shm = shared_memory.SharedMemory(name='foo', create=False)
    shmData = np.ndarray(shape, dtype, buffer=shm.buf)
    print('From read_from_shm():', shmData)
    return shm, shmData #we need to keep a reference of shm both so we don't
                        #  segfault on shmData and so we can `close()` it.

if __name__ == '__main__':
    data = np.arange(10)
    shm1 = store_in_shm(data)
    #This is where the *Windows* previously reclaimed the memory resulting in a 
    #  FileNotFoundError because the tempory mmap'ed file had been released.
    shm2, shmData = read_from_shm(data.shape, data.dtype)
    print('From __main__:', shmData)
    shm1.close() 
    shm2.close()
    #on windows "unlink" happens automatically here whether you call `unlink()` or not.
    shm2.unlink() #either shm1 or shm2

关于python - 使用 Python 共享内存的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63713241/

相关文章:

python - 从python中的视频中获取特定的帧序列

python - 堆叠分类器 : IndexError while fitting the data

python - 有没有办法记住 python 迭代器中的位置?

python - Blender3d 对比 3DS max;哪个更适合 python 中的自动化?

c++ - 是在 Mac 上使用 libc++ 或 libstdc++ 构建的库

macos - "Enter Full Screen"wxWidgets 中的热键

macos - 如何在 OS X 上使用 stat 命令来显示文件或目录的创建日期/时间?

Python 列表表示法,Numpy 数组表示法 : predictions[predictions < 1e-10] = 1e-10

python - 时间序列数据与numpy的自相关函数

Python 使用给定窗口运行累积和