[英]How to create a kafka consumer independent of Django server
I am fairly new to Python.我对 Python 相当陌生。 I have a django based web application where I am performing some tasks using Kafka.
我有一个基于 django 的 Web 应用程序,我正在使用 Kafka 执行一些任务。 I am using
confluent-kafka
wrapper to communicate with the Kafka Broker.我正在使用
confluent-kafka
包装器与 Kafka Broker 进行通信。
So once a topic is created on the kafka broker , I assign a consumer to subscribe to this process, to make this non-blocking, I am using multiprocessing
module so that the consumer runs on a separate thread and not block the main application thread.因此,一旦在kafka broker上创建了一个主题,我就会分配一个使用者来订阅该进程,以使其成为非阻塞的,我使用
multiprocessing
模块,以便使用者在单独的线程上运行而不阻塞主应用程序线程。
However, I am not sure, what will happen if my server shuts down?但是,我不确定,如果我的服务器关闭会发生什么? Will the process still run like how the cronjobs still run even if the server is not running?
即使服务器没有运行,该进程是否仍会像 cronjobs 一样运行?
If not can someone please suggest me how should I trigger my consumer so that it is independent of my django server?如果没有,请有人建议我应该如何触发我的消费者,使其独立于我的 Django 服务器?
If you need your web server to actually get the data from Kafka, they need to be running together.如果您需要 Web 服务器从 Kafka 中实际获取数据,则它们需要一起运行。
You probably don't want the consumer actively running in the background (as a separate process, not a thread) because then you're effectively not processing messages.您可能不希望消费者在后台主动运行(作为一个单独的进程,而不是一个线程),因为这样您实际上就没有处理消息。
Depending on what your data is, you could use Kafka Connect on the same machine as Django to write the Kafka events to a database, then Django would query the database instead根据您的数据是什么,您可以在与 Django 相同的机器上使用 Kafka Connect 将 Kafka 事件写入数据库,然后 Django 将查询数据库
bind the signals: signal.signal(signal.SIGTERM, signal_handler) signal.signal(signal.SIGINT, signal_handler) and use try-except blok and you can do a clean up afterwards.绑定信号:signal.signal(signal.SIGTERM, signal_handler) signal.signal(signal.SIGINT, signal_handler) 并使用 try-except blok 之后您可以进行清理。
to make consumer more independend, you can use django-celery-beat为了让消费者更加独立,你可以使用 django-celery-beat
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.