Trigger long process in Django view

I have a view (start_job_view) in Django that should trigger a very long process. I'd like the view to return immediately and add a message to the user ("your job has started" using the message framework) and I'd like that when the long process terminates it sends a new message to the user ("your job has finished").

How can I do this?

Note: I am not keen to add Celery to my project for this only task...

One option is to use async view. You can have more details over here

Another can be creating a celery task and calling it in your view.

You can use in-memory queue which runs on separate thread. Take a look at segment library code, which merge/process events in bg thread here.

Note that in case your server get terminated due to any reason you have mechanism to id job failed and retry.

Asuming you have multiple Django server instances running that connect to a dedicated DB server. I'd just use a database table as a "queue" to store the tasks that need to run.

Then use systemd to run a Django command as worker tasks on one or more server instances. When a worker is idle, it can query the table for the next task.

Have a "status" column to mark a task as done. Then your Django frontend can check for available "done" tasks, add the message to the returned page, and remove the task from the table.


def your_view(self, request):
   # your code          
   t = BackgroundTasksUtils(user) # Not required to pass parameter (it's up to you)
   return something


import threading

class BackgroundTasksUtils(threading.Thread):
   def __init__(self, user):
       super(BackgroundTasksUtils, self).__init__()
       self.user = user
   def run(self):
       # your code that taking too much time (in my case sending mail to user) 
   def __str__(self):
       return something

if you are not giving any parameter in then don't forgot to remove __init__ in

For a better and deeper understanding, visit threading

Back to Top