A
A
Arsen Abakarov2016-10-09 20:33:45
Django
Arsen Abakarov, 2016-10-09 20:33:45

Django + Celery file object serialization?

There is Django 1.10.2, Celery 3.1.24, openpyxl 2.4.0 the task is to generate an excel file asynchronously without saving and when ready to give a link to the client.
I did everything, I kick the url with Ajax, check the status of the task, if SUCCESS, then I give a link with the task ID
But all this does not work, since the file is contained in the BytesIO object, which is a byte stream, and this stream cannot be serialized for saving to the backend Celery what to do?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Arsen Abakarov, 2016-10-09
@ArsenAbakarov

I solved the issue myself, in the Celery task I return a string decoded in utf-8 encoding from BytesIO encoded in base64, then when I get the result, I first encode it in utf-8 and decode it from base64

A
Artem Sovetnikov, 2017-06-02
@Sovetnikov

The disadvantage of storing a large result of a Celery task is that the backends for storing results in Celery are not particularly designed for this, at least it is difficult to control the volume of results and clear them, there is no common API for managing, each backend has its own way.
In the case of Django, the easiest way is to use the cache (if the results are needed for a limited time), and only the cache entry key is passed to the task.
The only thing is that if the files are large, then it is worth setting up a separate cache specifically for transferring the contents of the files, otherwise useful data will be knocked out of the main cache.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question