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.