[Django]-Async functions in Django views

6👍

You can always fire off a thread:

import threading

class PreserializeThread(threading.Thread):
    def __init__(self, mquery_pk, company_uuid, *args, **kwargs):
        self.mquery_pk = mquery_pk
        self.company_uuid = company_uuid
        super(PreserializeThread, self).__init__(*args, **kwargs)

    def run(self):
        preserialize(self.mquery_pk, self.company_uuid)

Then, replace preserialize in your code sample with:

PreserializeThread(mquery.pk, company_uuid).start()

See also: http://docs.python.org/library/threading.html

1👍

Simple answer, no.

Your function will take until serialization is finished before continuing execution.

Have a look at django-celery for a task queuing solution.

As of 2020, celery now supports Django out of the box.

Leave a comment