Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
280 views
in Technique[技术] by (71.8m points)

python - How to set the size of ShareableList list in multiprocessing.shared_memory

I defined a ShareableList() to store strings in it by using the following code:

from multiprocessing import shared_memory

global_memory = shared_memory.ShareableList([""] * 10, name='my_mem')

global_memory[0] = "hello I'm a long string"

It returns:

ValueError: bytes/str item exceeds available storage

I expected this because in the documentation example the same happened:

>>> from multiprocessing import shared_memory

>>> a = shared_memory.ShareableList(['howdy', b'HoWdY', -273.154, 100, None, True, 42])

>>> [ type(entry) for entry in a ]
[<class 'str'>, <class 'bytes'>, <class 'float'>, <class 'int'>, <class 'NoneType'>, <class 'bool'>, <class 'int'>]

>>> a[2]
-273.154

>>> a[2] = -78.5

>>> a[2]
-78.5

>>> a[2] = 'dry ice'  # Changing data types is supported as well

>>> a[2]
'dry ice'

>>> a[2] = 'larger than previously allocated storage space'
Traceback (most recent call last):
  ...
ValueError: exceeds available storage for existing str

>>> a[2]
'dry ice'

len(a)
7

a.index(42)
6

a.count(b'howdy')
0

a.count(b'HoWdY')
1

a.shm.close()

a.shm.unlink()

del a  # Use of a ShareableList after call to unlink() is unsupported

But my question is, how to add more space for every element in the list, so I can store larger strings?

Thanks in advance.

question from:https://stackoverflow.com/questions/65858251/how-to-set-the-size-of-shareablelist-list-in-multiprocessing-shared-memory

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...