I have a tough time understanding how signals work into my application (and how they work period). These are three areas where I assume they would apply (with my current knowledge):
我很难理解信号如何进入我的应用程序(以及它们如何工作期)。这是我认为适用的三个方面(用我目前的知识):
- Send XML to a remote server for reporting (after a transaction is complete).
- 将XML发送到远程服务器以进行报告(在事务完成后)。
- Re size an image and upload the thumbnail to S3 after a user uploads it.
- 在用户上传图像后重新调整图像大小并将缩略图上传到S3。
- Delete old images from S3 after a user deletes an image object from his account.
- 用户从其帐户中删除图像对象后,从S3中删除旧图像。
Am I totally off base (I feel I might be). Am I getting signals and multi threading mixed up? If so, do they compare in there application? Are they only for decoupling? Also, what's the deal with making sure you instantiate them early and don't use a local function (because they'll get garbage collected)? Can someone elaborate on that? Should I put them all in request Middleware so that I don't have to worry?
我完全偏离基地(我觉得我可能会)。我收到信号和多线程混淆了吗?如果是这样,他们在应用程序中进行比较吗?它们只用于脱钩吗?另外,确保你提前实例化它们并且不使用本地函数(因为它们会收集垃圾)是什么?有人可以详细说明吗?我应该将它们全部放入请求中间件中,以便我不必担心吗?
3 个解决方案
#1
15
Django Signals are a way to perform an action A
in response to an event E
.
Django信号是一种响应事件E执行动作A的方法。
In a unreal world you can avoid using signals by modifying the code where the event E
occurs and appending the code to perform the action A
.
在虚幻的世界中,您可以通过修改发生事件E的代码来避免使用信号,并附加代码来执行操作A.
The problem is that doing so you loose maintainability, readability and a lot of other software engineering adjectives :)
问题是,这样做会破坏可维护性,可读性和许多其他软件工程形容词:)
Signals allow you to do the same thing indipendently from where or how the event E
occurs and so doing so in a clever way that allow maintanability, readability, etc...
信号允许您独立地从事件E发生的位置或方式做同样的事情,因此以巧妙的方式这样做,允许可维护性,可读性等...
Yes, I think that saying that Signals are useful to enable decoupling is really true.
是的,我认为说信号对于实现解耦是有用的,这是真的。
(You also mentioned multi threading. If you did so because you think signals are good because they are executed concurrently and so quickly... Well... I don't know if they are concurrently executed but anyway I really don't think this is the point for what django signals are useful for)
(你也提到了多线程。如果你这样做是因为你认为信号是好的,因为它们是同时执行的那么快......嗯......我不知道它们是否同时被执行但不管怎么说我真的不认为这是django信号有用的重点)
An example of a good way of taking advantage of Signals is about the fact that when you want to store other information to an user in django you have to use Userprofiles. In this case, the documentation itself, tell you that it may be convenient to register a signal in response to any creation of new users just to add to the new created users an empty user profile.
利用信号的好方法的一个例子是,当你想在django中向用户存储其他信息时,你必须使用Userprofiles。在这种情况下,文档本身告诉您注册信号以响应任何新用户的创建可能是方便的,只是为了向新创建的用户添加空用户配置文件。
#2
3
Here is an example that may help.
这是一个可能有帮助的例子。
Suppose you need to perform some action when a model instance is saved. However, this action has got nothing to do with the model or model instance itself directly. Therefore it makes little sense to put the code for your action in a save() method on the model. It would cause unnecessary code coupling and clutter. Instead you can create a signal handler somewhere else in your application (or even in another application) where it makes more sense.
假设您需要在保存模型实例时执行某些操作。但是,此操作与模型或模型实例本身无直接关系。因此,将您的操作代码放在模型上的save()方法中是没有意义的。这会导致不必要的代码耦合和混乱。相反,您可以在应用程序中的其他位置(甚至在其他应用程序中)创建信号处理程序,使其更有意义。
#3
1
I would perform Tasks 2 and 3(the images stuff) with something like an asynchronous task queue, like Celery
我会用类似异步任务队列(如Celery)执行任务2和3(图像内容)
That's similar to multithreading.
这与多线程类似。
#1
15
Django Signals are a way to perform an action A
in response to an event E
.
Django信号是一种响应事件E执行动作A的方法。
In a unreal world you can avoid using signals by modifying the code where the event E
occurs and appending the code to perform the action A
.
在虚幻的世界中,您可以通过修改发生事件E的代码来避免使用信号,并附加代码来执行操作A.
The problem is that doing so you loose maintainability, readability and a lot of other software engineering adjectives :)
问题是,这样做会破坏可维护性,可读性和许多其他软件工程形容词:)
Signals allow you to do the same thing indipendently from where or how the event E
occurs and so doing so in a clever way that allow maintanability, readability, etc...
信号允许您独立地从事件E发生的位置或方式做同样的事情,因此以巧妙的方式这样做,允许可维护性,可读性等...
Yes, I think that saying that Signals are useful to enable decoupling is really true.
是的,我认为说信号对于实现解耦是有用的,这是真的。
(You also mentioned multi threading. If you did so because you think signals are good because they are executed concurrently and so quickly... Well... I don't know if they are concurrently executed but anyway I really don't think this is the point for what django signals are useful for)
(你也提到了多线程。如果你这样做是因为你认为信号是好的,因为它们是同时执行的那么快......嗯......我不知道它们是否同时被执行但不管怎么说我真的不认为这是django信号有用的重点)
An example of a good way of taking advantage of Signals is about the fact that when you want to store other information to an user in django you have to use Userprofiles. In this case, the documentation itself, tell you that it may be convenient to register a signal in response to any creation of new users just to add to the new created users an empty user profile.
利用信号的好方法的一个例子是,当你想在django中向用户存储其他信息时,你必须使用Userprofiles。在这种情况下,文档本身告诉您注册信号以响应任何新用户的创建可能是方便的,只是为了向新创建的用户添加空用户配置文件。
#2
3
Here is an example that may help.
这是一个可能有帮助的例子。
Suppose you need to perform some action when a model instance is saved. However, this action has got nothing to do with the model or model instance itself directly. Therefore it makes little sense to put the code for your action in a save() method on the model. It would cause unnecessary code coupling and clutter. Instead you can create a signal handler somewhere else in your application (or even in another application) where it makes more sense.
假设您需要在保存模型实例时执行某些操作。但是,此操作与模型或模型实例本身无直接关系。因此,将您的操作代码放在模型上的save()方法中是没有意义的。这会导致不必要的代码耦合和混乱。相反,您可以在应用程序中的其他位置(甚至在其他应用程序中)创建信号处理程序,使其更有意义。
#3
1
I would perform Tasks 2 and 3(the images stuff) with something like an asynchronous task queue, like Celery
我会用类似异步任务队列(如Celery)执行任务2和3(图像内容)
That's similar to multithreading.
这与多线程类似。