Django:信号到底有什么用处?

发布于 2024-08-18 09:37:20 字数 359 浏览 5 评论 0原文

我很难理解信号如何在我的应用程序中工作(以及它们如何工作)。我认为它们将适用于以下三个领域(根据我目前的知识):

  1. 将 XML 发送到远程服务器进行报告(在事务完成后)。
  2. 用户上传图像后,重新调整图像大小并将缩略图上传到 S3。
  3. 用户从其帐户中删除图像对象后,从 S3 中删除旧图像。

我是不是完全偏离了基地(我觉得我可能是)。 我是否混淆了信号和多线程?如果是这样,它们在应用程序中进行比较吗?它们只是为了解耦吗?另外,如何确保尽早实例化它们并且不使用本地函数(因为它们会被垃圾收集)?有人可以详细说明一下吗?我是否应该将它们全部放在 request 中间件中以便我不必担心?

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):

  1. Send XML to a remote server for reporting (after a transaction is complete).
  2. Re size an image and upload the thumbnail to S3 after a user uploads it.
  3. Delete old images from S3 after a user deletes an image object from his account.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

春风十里 2024-08-25 09:37:20

Django 信号是一种执行操作 A 以响应事件 E 的方法。

在虚幻世界中,您可以通过修改发生事件 E 的代码并附加执行操作 A 的代码来避免使用信号。

问题是,这样做会失去可维护性、可读性和许多其他软件工程形容词:)

信号允许您独立于事件 E 发生的位置或方式来执行相同的操作,因此在一种允许可维护性、可读性等的巧妙方法...

是的,我认为信号对于启用解耦有用的说法确实是正确的。

(你还提到了多线程。如果你这样做是因为你认为信号很好,因为它们是并发执行的,而且速度很快......好吧......我不知道它们是否是并发执行的,但无论如何我真的不认为这是 django 信号的有用之处)

利用信号的一个好方法的例子是,当您想在 django 中向用户存储其他信息时,您必须使用用户配置文件。
在这种情况下,文档本身,告诉您,注册一个信号来响应任何新用户的创建可能会很方便,只是为了向新创建的用户添加一个空的用户配置文件。

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.

征棹 2024-08-25 09:37:20

这是一个可能有帮助的示例。

假设您需要在保存模型实例时执行某些操作。但是,此操作与模型或模型实例本身没有直接关系。因此,将操作代码放入模型的 save() 方法中没有什么意义。这会导致不必要的代码耦合和混乱。相反,您可以在应用程序中的其他位置(甚至在另一个应用程序中)创建更有意义的信号处理程序。

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.

花开半夏魅人心 2024-08-25 09:37:20

我会使用异步任务队列之类的东西来执行任务 2 和 3(图像内容),例如 Celery

这类似于多线程。

I would perform Tasks 2 and 3(the images stuff) with something like an asynchronous task queue, like Celery

That's similar to multithreading.

一念一轮回 2024-08-25 09:37:20

信号不是异步的(不以并发或并行方式运行)

这就是为什么它们对于您提到的事情那么有用:

  • 文件上传或文件后处理
  • 报告生成
  • 任何其他长期操作(请求另一个服务器,ET)

更好的 Celery 工作人员可以以真正异步的方式进行长期操作。

但它们在一些有限的场景中仍然很有价值

  • 扩展第三方库的功能。如果您使用的应用程序具有无法控制代码的模型,则信号是比猴子修补更强大的替代方案,用于在保存或删除时执行特定操作。
  • 删除信号适用于删除查询集方法。如果您希望在单个对象和查询集删除上发生相同的操作,那么信号可能是您最好的选择。
    注意:由于 SQL 的生成方式,这不适用于更新查询集方法(带有保存信号)。另一个常见的混乱/错误来源。
  • 当您需要将相同的信号处理程序应用于多个模型时。如果您只有几个,我仍然倾向于覆盖保存/删除方法并调用共享函数。对于很多人来说,在任何地方复制/粘贴保存方法变得更容易出错,而信号看起来是更好的选择。
  • 避免严格的跨应用程序依赖。如果您需要跨越应用程序边界,特别是当应用程序可能在多个项目中使用时,信号可能更适合应用程序之间的松散耦合。但要小心这个。使用它是因为您需要,而不是因为您认为将来可能会需要它。

您可以在本主题中找到这些案例及其一些示例。

Signals are NOT asynchronous (NOT running in concurrent or parallel way)

That's why they are NOT so useful for things like you mentioned:

  • File upload or file post-processing
  • Reports generation
  • Any other long term operations (requests to another servers, ET)

It is better Celery workers that can do long term operations in really asynchronous way.

But they are still valuable in a few limited scenarios

  • Extending the functionality of a third-party library. If you're using an app with models where you don't control the code, signals are a more robust alternative to monkey-patching for taking specific actions on save or delete.
  • Delete signals work on the delete queryset method. If you'd like the same actions to happen on both single object and queryset deletes, a signal is probably your best bet.
    Note: Due to the way the SQL is generated, this does not apply to the update queryset method (with save signals). Another frequent source of confusion/bugs.
  • When you need to apply the same signal handler to many models. If you just have a few, I'd still favor overriding the save/delete method and calling a shared function. For more than a few, copy/pasting the save method everywhere becomes more error-prone and signals look like a better option.
  • Avoiding tight cross-application dependencies. If you need to cross application boundaries, especially when the application may be used in multiple projects, signals may be better for looser coupling between the apps. Be careful with this one though. Use it because you need to, not because you think you might want it in the future.

These cases and some examples for them you can find in this topic.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文