shahriyardx / easy-pil

A library to make common tasks of Pillow easy.
https://easy-pil.readthedocs.io/
MIT License
39 stars 15 forks source link

Cache Expiry and Persistence for "load_image_async" Function #36

Open munishkhatri720 opened 2 months ago

munishkhatri720 commented 2 months ago

Does this will not increase memory usage of the application in which easy_pil is used ? image

I have an api which builds musiccard every second and I noticed my vps memory usage touched 1gb in just few minutes image

shahriyardx commented 2 weeks ago

Are you closing the editor after generating images ?

munishkhatri720 commented 2 weeks ago

Yes, I am closing the editor after modifying the image. But my question is, why is the cache TTL set to 12 hours? I used easy_pil with FastAPI, which handles 22,000 images every day, but the API's RAM usage reached 7 GB in a single day. I think the load_image_async function is caching images for too long, which is causing the high RAM usage.

shahriyardx commented 2 weeks ago

So, do you think removing cache is better so you can implement your own caching strategy? Also load_image loads the image from a link then opens it using Image.open() you need to close this too after the usage of the image is done

editor = Editor()

img = await load_image_async(url)
editor.paste(img, ...)
img.close()

bytes = editor.image_bytes
editor.close()

Let me know if this strategy helps reducing ram usage, ultimately all the images that you open must be closed to improve performance