TianTian Fu
TianTian Fu

Reputation: 21

How can I run a background-task in django and when it's done, I can push information to front end.

Since one of my tasks in views.py is time-consuming, so I think I'd better put it in the background. But also, I want to make sure when this task finished, I'll receive something in the front end. How can I achieve this? I've searched and found django-channels, but still, I can't combine the two goals together. Hope someone will help me.

Upvotes: 2

Views: 2034

Answers (2)

naren
naren

Reputation: 15233

Always run background tasks using Asynchronous task processing like

  1. CELERY
  2. DJANGO BACKGROUND TASKS (https://github.com/arteria/django-background-tasks)

For pushing notification use

  1. Django-channels(websockets)
  2. Django-webpush https://github.com/safwanrahman/django-webpush
  3. Polling
  4. Tornado(long opening connection) or StreamingHttpResponse in Django can solve also

if you think websockets are difficult for you to maintain go for polling.

Upvotes: 1

schacki
schacki

Reputation: 9533

You basically have 2 options:

Either you have your client request the status of your long-running task regularly and respond accordingly when it is done.

Or you use sockets between your client and server and inform your client via the socket, when the task is finished. One of the recommend options for sockets is django-channels. Is there anything wrong with it?

Upvotes: 2

Related Questions