Passing an image to a celery task

我们两清 提交于 2019-12-10 19:02:17

问题


Part of an application I'm writing allows for users to upload images which I then resize and automatically upload to amazon s3. Currently the image resizing is happening right in the view and I'd like to offload this via celery to distributed workers. My question is whats the best way to get the image to the worker. My current thinking is to store the image directly in the database and then just pass the id to the worker and have it retrieve it. Is there a better practice then temporarily storing it in the database until it can get processed?


回答1:


As stated in the celery docs, it is better not to pass the whole thing (image) as an argument, for this will cause extra overhead. So it is better to save it in first place, then pass the photo id as an argument, retrieve the image into the task and do the resizing / uploading.




回答2:


You can't pass the actual image to Celery because the image cannot be pickled, however you can deconstruct the image and send all it's data to a celery worker, where you can easily rebuild the image and then do whatever you want with it.

I build a small app which offloads the saving of an image to a worker, it would not be too difficult to fork it and add you own resizing to it. Check it out https://github.com/gterzian/django_async



来源:https://stackoverflow.com/questions/9560611/passing-an-image-to-a-celery-task

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!