在Python3.8中使用多进程共享内存分享bytes对象。

3
我有一个很大的字节对象,想将其放入共享内存中,以便我的多进程任务可以访问它。我正在使用ShareableList,如文档所述。
from multiprocessing import shared_memory
s2v_a = Sense2Vec().from_disk(SENSE2VEC_FOLDER)
s2v_a_bytes = s2v_a.to_bytes()
print(sys.getsizeof(s2v_a_bytes)) #prints <class 'bytes'>
print(type(s2v_a_bytes)) #prints 4220733334 (4.2Gb)
memory = shared_memory.ShareableList([s2v_a_bytes])

然而,当我尝试创建ShareableList时,我收到一个断言错误,说明格式不小于8。我可以看出这与结构打包格式有关。
Traceback (most recent call last):
  File "/home/user/anaconda3/envs/uniqueness/lib/python3.8/runpy.py", line 193, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/user/anaconda3/envs/uniqueness/lib/python3.8/runpy.py", line 86, in _run_code
    exec(code, run_globals)
  File "/home/user/dev/uniqueness/backend/app/coding.py", line 39, in <module>
    memory = shared_memory.ShareableList([s2v_a_bytes])
  File "/home/user/anaconda3/envs/uniqueness/lib/python3.8/multiprocessing/shared_memory.py", line 295, in __init__
    assert sum(len(fmt) <= 8 for fmt in _formats) == self._list_len
AssertionError

代码中的注释

Because values are packed into a memoryview as bytes, the struct
    packing format for any storable value must require no more than 8
    characters to describe its format."""

但据我所知,我没有做任何与文档不同的事情。

1个回答

1
文档中指出在ShareableList中,bytesstr的最大大小为10M字节。4.2GB远超过此限制。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接