Django中的信号性能 [英] Performance of signals in Django

查看:158
本文介绍了Django中的信号性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一些 post_save 信号,但我想知道以后是否会出现性能问题。例如,我有这样的东西:

  def my_signal(发送者,** kwargs):
#处理
posts = len(MyPosts.objects.filter(date__gte = variable))
如果条目==20:
#打包一个对象并将其分配给帖子的作者。


post_save.connect(my_signal,sender = MyPosts)

假设我有一个非常繁忙的网站,每次创建一个帖子时都会触发。对性能来说太糟糕了吗?有没有办法以更缓慢的方式发射信号(也许每天或每几个请求)?



更新另外一个问题是:Django足够智能地将 .save()调用与 post_save 信号转换成一个数据库请求,还是在这里做几个请求?



谢谢!

解决方案

您的信号处理程序的性能影响当然取决于其功能。但是您应该注意到,当信号被触发时,它们是同步执行的,所以如果处理程序中有很多操作(例如大量的数据库调用),执行将会被延迟。



Django本身不会为您提供集成信号处理程序的另一个机会,但如有必要,您应该看看 django-celery ,这应该使您能够从信号处理程序中异步调用任务,例如...


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!

解决方案

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...

这篇关于Django中的信号性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆