Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Performance of signals in Django

I have created a few post_save signals but I was wondering if there will be performance issues later on. For example, I have something like this:

def my_signal(sender, **kwargs):
  # some minimal processing
  posts = len(MyPosts.objects.filter(date__gte=variable))
  if entries == "20":
    # crate an object and assign it to the post's author.


post_save.connect(my_signal, sender=MyPosts)

Let's say I have a very busy site and this is firing each time a post is created. Is it too bad to performance?. Is there a way to fire signals in a more slow-paced way (maybe, once a day or every few requests)?.

UPDATE:

Another question: is Django smart enough to compound the .save() call with the post_save signal into one database request or is it doing a couple of requests here?.

Thanks!

like image 616
Robert Smith Avatar asked May 31 '12 19:05

Robert Smith


1 Answers

The performance impact of your signal handlers depends of course on their functionality. But you should be aware of the fact that they are executed synchronously when the signal is fired, so if there's a lot of action going on in the handlers (for example a lot of database calls) the execution will be delayed.

Django itself doesn't offer you another opportunity to integrate signal handlers, but if necessary you should have a look in to django-celery which should enable you to call tasks asynchronously from within a signal handler for example...

like image 89
Bernhard Vallant Avatar answered Oct 05 '22 11:10

Bernhard Vallant