使用Python共享内存共享两个过程之间的数据

发布于 2025-02-06 15:49:25 字数 1602 浏览 0 评论 0原文

大家好(预先感谢) 问题背景:

  1. 我有一个将图像(带有枕头)加载到内存中的
  2. 烧瓶 目的。 我不确定实现这一目标的最佳方法,但我尝试使用此 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:

  1. I have a Flask which loads image (with Pillow) in to the memory
  2. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文