Django asynchronous – on model save function

Question:

I have the following:

def save(self):
    for lang in ["es", "ar"]:
        setattr(self, "title_" + lang, translateField(self.title, lang))
    super(Landmarks, self).save()

The translateField function calls Microsoft translator API, which takes some time to finish executing.

Is it possible to do the same asynchronously?

Asked By: user1301404

||

Answers:

Django is not an asynchronous framework.

You need an asynchronous job/task queue, like celery:

Celery is an asynchronous task queue/job queue based on distributed
message passing. It is focused on real-time operation, but supports
scheduling as well.

django-celery package makes it easier to integrate django and celery.

There is also a Redis Queue project:

RQ (Redis Queue) is a simple Python library for queueing jobs and
processing them in the background with workers. It is backed by Redis
and it is designed to have a low barrier to entry. It should be
integrated in your web stack easily.

Also see:

Answered By: alecxe
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.