我读到了RawArray
可以在进程之间共享而无需复制,并且想了解它在Python中是如何实现的。
我看到在共享ctypes.py https://github.com/python/cpython/blob/master/Lib/multiprocessing/sharedctypes.py,那一个RawArray
是由一个BufferWrapper
from heap.py https://github.com/python/cpython/blob/master/Lib/multiprocessing/heap.py,然后用ctypes.memset
.
BufferWrapper
是由一个Arena
对象,它本身是由mmap
(或者 Windows 中的 100 个 mmap,请参见第 40 行heap.py https://github.com/python/cpython/blob/master/Lib/multiprocessing/heap.py)
我读到mmap
Linux/BSD中系统调用实际上是用来分配内存的,Python模块使用文件映射视图 https://learn.microsoft.com/en-us/windows/desktop/api/memoryapi/nf-memoryapi-mapviewoffile对于窗户。
mmap
那么看起来很方便。貌似可以直接使用mp.pool
-
from struct import pack
from mmap import mmap
def pack_into_mmap(idx_nums_tup):
idx, ints_to_pack = idx_nums_tup
pack_into(str(len(ints_to_pack)) + 'i', shared_mmap, idx*4*total//2 , *ints_to_pack)
if __name__ == '__main__':
total = 5 * 10**7
shared_mmap = mmap(-1, total * 4)
ints_to_pack = range(total)
pool = Pool()
pool.map(pack_into_mmap, enumerate((ints_to_pack[:total//2], ints_to_pack[total//2:])))
我的问题是 -
多处理模块如何知道不复制mmap
based RawArray
进程之间的对象,就像“常规”Python 对象一样?