简体   繁体   English

如何在 Django 应用程序之外使用 Django 信号?

[英]How can I use Django signals outside the Django application?

I have a gRPC server running that responds to external requests.我正在运行一个响应外部请求的 gRPC 服务器。 I also have a Django app that takes care of all other web related events.我还有一个 Django 应用程序,它负责处理所有其他 web 相关事件。 I want to send a signal to the Django app when the gRPC server receives requests.当 gRPC 服务器接收到请求时,我想向 Django 应用程序发送信号。 (The gRPC system was already in place. The Django app is an extension) (gRPC 系统已经到位。Django 应用程序是一个扩展)

Since using Django signals outside the app doesn't work (no context), I want another way to send some kind of notification to the app.由于在应用程序外部使用 Django 信号不起作用(没有上下文),我想要另一种方式向应用程序发送某种通知。

I am aware that this is a very unlikely scenario.我知道这是一个非常不可能的情况。

I implemented the first method.我实现了第一种方法。 There could be other ways too.也可能有其他方法。

  1. Works.作品。 If you insist on using Django signals, you can start the gRPC server as a thread in asgi.py.如果您坚持使用 Django 信号,您可以在 asgi.py 中将 gRPC 服务器作为线程启动。 This allows it to gain the Django context and you can then create custom signals and use it in your gRPC server code.这允许它获得 Django 上下文,然后您可以创建自定义信号并在您的 gRPC 服务器代码中使用它。
  2. By simply using a library like requests in Python, you can create a communication mechanism to reach your Django app.通过简单地使用 Python 中的请求之类的库,您可以创建一种通信机制来访问您的 Django 应用程序。

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

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