Using temporary files in Django Celery results

I am using the celery task to create PDF and Excel export for user data. These tasks create temporary files. Here is the problem. I use NamedTemporaryFile objects that are automatically deleted when the file is closed. Since they are closed at the end of the task, they simply disappear immediately.

I can make it work if I set the delete property to false:

NamedTemporaryFile(delete=False) 

But we have a problem to leave a bunch of temporary files in the system, which I don’t want.

I could always create a cleaning task, but I was hoping there would be a better sample ...

Thanks!

+4
source share
1 answer

You can use the StringIO / CStringIO class instead of the NamedTemporaryFile to capture data from an Excel / PDF file and return its value. The StringIO class behaves just like a regular file.

0
source

Source: https://habr.com/ru/post/1486080/


All Articles