简体   繁体   English

Django Signal与Python线程

[英]Django Signal vs Python Threading

Its hard to explain what I am trying to achieve. 很难解释我想要达到的目标。 Please have the patience to go through this. 请耐心等待。 And let me know if you have any questions. 如果您有任何疑问,请告诉我。

Say I have a Django project with two applications which I would like to have them coupled loosely. 假设我有一个包含两个应用程序的Django项目,我希望它们可以松散地耦合。 One of the application is 'Jobs' and other is 'Notifications'. 应用程序之一是“工作”,其他应用程序是“通知”。

Now I want to create notifications when the Job instance is updated. 现在,我想在Job实例更新时创建通知。 So, I was thinking of using Django Signals. 因此,我正在考虑使用Django Signals。 But some of the reservations I have are: 但是我有一些保留意见:

  1. If I use the build-in signals like post_save. 如果我使用诸如post_save之类的内置信号。 I could validate the conditions on job instance and generate notification(which is good). 我可以验证作业实例上的条件并生成通知(很好)。 But the problem comes when, in the same view logic I call the save method on job instance multiple times. 但是问题出在同一工作逻辑中,我多次在作业实例上调用save方法时。 This would generate notifications multiple times. 这将多次生成通知。 Else, I use the home made signals I would be required to call it manually which is not good for loose coupling. 否则,我需要使用自制的信号来手动调用它,这对松耦合不利。
  2. Moreover, the signals are not asynchronous so, I would have to wait for the notification generation to complete before I can proceed. 而且,信号不是异步的,所以我必须等待通知生成完成才能继续。

Can anyone please suggest a good implementation strategy using Signals. 任何人都可以提出使用Signals的良好实施策略的建议。 One solution I was looking into was Python Threading which seems to take care of the asynchronous problem. 我正在研究的一种解决方案是Python线程,它似乎可以解决异步问题。 But are there any other consequences of using Threading. 但是使用线程还会有其他后果。

I would suggest you to use django-celery with RabbitMQ. 我建议您在RabbitMQ中使用django-celery。 You can add the notifications thing in the tasks of celery and have your view start the task queque. 您可以在celery任务中添加通知内容,并让视图启动任务队列。 Have a look....I hope it will be helpful to you. 看看...。希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM