在 Python3.8 的多进程共享内存中共享字节对象
Sharing a bytes object in Multiprocess SharedMemory in Python3.8
我有一个大字节对象,我想将其放入 SharedMemory 以便我的多处理任务可以访问它。我正在使用 docs.
中描述的 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。我可以看出这与 struct packing format.
有关
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."""
但据我所知,我没有做任何与文档不同的事情。
文档说明 bytes
和 ShareableList
中的 str
的最大大小为 10M 字节。 4.2 GB 远远超过了这个限制。
我有一个大字节对象,我想将其放入 SharedMemory 以便我的多处理任务可以访问它。我正在使用 docs.
中描述的 ShareableListfrom 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。我可以看出这与 struct packing format.
有关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."""
但据我所知,我没有做任何与文档不同的事情。
文档说明 bytes
和 ShareableList
中的 str
的最大大小为 10M 字节。 4.2 GB 远远超过了这个限制。