Is Django post_save signal asynchronous?

Bastian picture Bastian · Aug 10, 2012 · Viewed 14.5k times · Source

I have a like function which is just like social networks like or thumbs up function; the user clicks the star / heart / whatever to mark the content as liked.It is done with ajax and must be fast.

The only problem here is that for some reasons I have to do some tasks for each like and I found out they were coded straight in the like view and it makes it slow.

I am thinking of using signals to make the execution of these tasks asynchronous so the view can send back the json right away to the javascript without waiting for the tasks to finish.

I started creating a signal for the like but then realized that Django's signals were not asynchronous and it would end up the same, the view would have to wait for the signal to finish to send back its response.

So I could try to make that signal asynchronous as it is explained here and there but I would as well use the post_save signal for the like model but now I wonder if the view can finish before the signal gets executed?

Answer

Bouke picture Bouke · Aug 11, 2012

Also look into celery (or more specifically django-celery). It is an async task scheduler / handler. So your post_save signal handler creates a task, which is picked up and executed through celery. That way you still have your speedy application, while the heavy lifting is performed async, even on a different machine or batch of machines.