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):
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?
The only reason to use signalsOnly use signals to avoid introducing circular dependencies. If you have two apps, and one app wants to trigger behaviour in an app it already knows about, don't use signals. The app should just import the function it needs and call it directly.
First, to dispel a misconception about signals, they are not executed asynchronously. There is no background thread or worker to execute them. Like most of Django, they are fully "synchronous".
To notify another part of the application after the delete event of an object happens, you can use the post_delete signal.
Django signals allow listeners to be registered for events within the framework. This allows decoupled handling of, for example, model deletions.
Django Signals are a way to perform an action A
in response to an event E
.
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
.
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...
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With