Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is Django post_save signal asynchronous?

Tags:

django

signals

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?

like image 910
Bastian Avatar asked Aug 10 '12 09:08

Bastian


People also ask

Is Django Async ready?

Django has support for writing asynchronous (“async”) views, along with an entirely async-enabled request stack if you are running under ASGI. Async views will still work under WSGI, but with performance penalties, and without the ability to have efficient long-running requests.

Are signals asynchronous?

A signal is an asynchronous notification sent to a process or to a specific thread within the same process in order to notify it of an event that occurred.

What kind of signals are there in Django?

There are 3 types of signal. pre_save/post_save: This signal works before/after the method save(). pre_delete/post_delete: This signal works before after delete a model's instance (method delete()) this signal is thrown.

What is the use of the Post_delete signal in Django?

Django Signals - post_delete()To notify another part of the application after the delete event of an object happens, you can use the post_delete signal.


2 Answers

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.

like image 108
Bouke Avatar answered Sep 20 '22 19:09

Bouke


What you want is a thread. They're very easy to use. You just subclass threading.Thread and write a run method:

import threading  class LikeThread(threading.Thread):     def __init__(self, user, liked, **kwargs):         self.user = user         self.liked = liked         super(LikeThread, self).__init__(**kwargs)      def run(self):         # long running code here 

Then, when your ready to do the task, you fire it off with:

LikeThread(request.user, something).start() 

The rest of your view code or whatever will resume and return the response, and the thread will happily do its work until it's done and then end itself.

See full documentation: http://docs.python.org/library/threading.html

like image 21
Chris Pratt Avatar answered Sep 21 '22 19:09

Chris Pratt