首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Python3.8中的多进程SharedMemory中共享字节对象

在Python3.8中的多进程SharedMemory中共享字节对象
EN

Stack Overflow用户
提问于 2020-04-20 17:09:06
回答 1查看 229关注 0票数 2

我有一个很大的字节对象,我想把它放在SharedMemory中,这样我的多处理任务就可以访问它。我使用的是docs中描述的ShareableList。

代码语言:javascript
复制
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的AssertionError。我可以看出这与struct packing format有关。

代码语言:javascript
复制
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

代码中的注释

代码语言:javascript
复制
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."""

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-23 01:23:30

文档规定bytesstrShareableList中的最大大小是10M字节。4.2 GB远远超出了这个限制。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61318799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档