使用Python共享内存共享两个过程之间的数据
大家好(预先感谢) 问题背景:
- 我有一个将图像(带有枕头)加载到内存中的
- 烧瓶 目的。 我不确定实现这一目标的最佳方法,但我尝试使用此 https:https:https:https: //github.com/luizalabs/shared-memory-dict
这是父母/孩子简化的示例:
父:
import time
from shared_memory_dict import SharedMemoryDict
from PIL import Image
if __name__ == '__main__':
print("Starting PARENT")
source_img = Image.open('d:/input_image.png')
size = 1024 * 1024 * 16
smd = SharedMemoryDict(name='shared_mem', size=size)
smd['source_img'] = source_img
print("PARENT started!")
source_img.show()
time.sleep(99999)
孩子:
from shared_memory_dict import SharedMemoryDict
if __name__ == '__main__':
print("Starting CHILD")
size = 1024 * 1024 * 16
smd = SharedMemoryDict(name='shared_mem', size=size)
existing_smd = SharedMemoryDict(name='shared_mem', size=size)
source_img = existing_smd['source_img']
source_img.show()
我第一次运行它(跑步父母,然后是孩子) - 一切正常工作 但是第二个孩子的跑步因这个错误而失败:
Starting CHILD
Traceback (most recent call last):
File "child_dict.py", line 23, in <module>
source_img = existing_smd['source_img']
File "/data/anaconda3/envs/STR_3_8/lib/python3.8/site-packages/shared_memory_dict/dict.py",
line 92, in __getitem__
return self._read_memory()[key]
KeyError: 'source_img'
因此,看起来像是在运行子女之后 - pIL对象已从共享emememorydict中删除。
欢迎任何想法在通过多个子过程重新使用的共享内存中进行相同的条目。
Hello ALL ( and thanks in advance)
Problem background:
- I have a Flask which loads image (with Pillow) in to the memory
- For each request I would like to create a child process which will use loaded into memory Pil
object.
I'm not sure regarding best way to achieve that but I've tried to use this one https://github.com/luizalabs/shared-memory-dict
Here is parent/child simplified example:
Parent:
import time
from shared_memory_dict import SharedMemoryDict
from PIL import Image
if __name__ == '__main__':
print("Starting PARENT")
source_img = Image.open('d:/input_image.png')
size = 1024 * 1024 * 16
smd = SharedMemoryDict(name='shared_mem', size=size)
smd['source_img'] = source_img
print("PARENT started!")
source_img.show()
time.sleep(99999)
Child:
from shared_memory_dict import SharedMemoryDict
if __name__ == '__main__':
print("Starting CHILD")
size = 1024 * 1024 * 16
smd = SharedMemoryDict(name='shared_mem', size=size)
existing_smd = SharedMemoryDict(name='shared_mem', size=size)
source_img = existing_smd['source_img']
source_img.show()
Once I run it for the first time (running parent , then the child) - everything works normaly
But second child run fails with this error:
Starting CHILD
Traceback (most recent call last):
File "child_dict.py", line 23, in <module>
source_img = existing_smd['source_img']
File "/data/anaconda3/envs/STR_3_8/lib/python3.8/site-packages/shared_memory_dict/dict.py",
line 92, in __getitem__
return self._read_memory()[key]
KeyError: 'source_img'
So it looks like after running child - the PIL object is removed from SharedMemoryDict.
Any idea how to make same entry in shared memory re-usable by multiple child processes is welcome.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论