简体   繁体   English

如何创建一个独立于 Django 服务器的 kafka 消费者

[英]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.

相关问题 Django-如何创建具有独立列的架构 - Django- how to create schemas with independent columns kafka使用者,独立服务或Django组件的作用? - Role of kafka consumer, seperate service or Django component? 如何在 Django 中创建独立于用户权限的权限表? - How to create a permissions table independent of the user permissions in Django? 如何将 Kafka 消费者连接到 Django 应用程序? 我应该为消费者或新进程或新的 docker 容器使用新线程吗? - How to connect Kafka consumer to Django app? Should I use a new thread for consumer or new process or new docker container? django如何使django执行用户独立或会话独立的任务? - How to make django do user independent or session independent tasks? 如何使Django模块完全独立? - how to make django module fully independent? 如何为Django项目创建模拟LDAP服务器? - How to create mock LDAP server for Django project? 如何在 Django 应用程序中实现 Kafka? - How to implement Kafka in a Django application? Django 频道是否会在每次使用时创造新的消费者? - Does Django Channels create a new consumer with every use? 如何从 django 视图向消费者(django-channels)发送消息? - How to send message from django view to consumer (django-channels)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM