1

I'm trying to execute a long running function (ex: sleep(30)) after a Django view returns a response. I've tried implementing the solutions suggested to similar questions:

However, the client's page load only completes after the long running function completes running when using a WSGI server like gunicorn.

Now that Django supports asynchronous views is it possible to run a long running query asynchronously?

NFern
  • 1,706
  • 17
  • 18
  • [This solution](https://stackoverflow.com/questions/62390314/how-to-call-asynchronous-function-in-django) is worked for me. – Reşat ARIKAN May 08 '22 at 23:46

1 Answers1

0

Obviously, I am looking for a solution regarding the same issue, to open a view which should start a background task and send a response to the client without waiting until started task is finished. As far as I understand yet this is not one of the objectives of async view in Django. The problem is that all executed code is connected the the worker started to handle the http request. If the response is sent back to the client the worker cannot handle any other code / task anymore started in the view asynchronous. Therefore, all async functions require an "await" in front of. Consequently, the view will only send its response to the client if the awaited function is finished.

As I understand all background tasks must be pushed in a queue of tasks where another worker can catch each new task. There are several solution for this, like Djangp Channels or Django Q. However, I am not sure what is the most lightweighted solution.

Mike42
  • 166
  • 2
  • 9
  • Thanks for the answer @Mike42! I am currently using django-q, but wanted something that does not need an additional service to run. Looks like asyc views is not the answer. – NFern Aug 10 '20 at 22:11