I have been trying to build a Flask app that has Kafka as the only interface. For this reason, I want have a Kafka consumer that is triggered when there is new message in the stream of the concerned topic and respond by pushing messages back to the Kafka stream.
I have been looking for something like the Spring implementation:
@KafkaListener(topics = "mytopic", groupId = "mygroup")
public void listen(String message) {
System.out.println("Received Messasge in group mygroup: " + message);
}
I have looked at:
But I couldn't find anything related to event-driven style of implementation in Python.
Kafka Consumer have to continuously poll to retrieve data from brokers.
Spring gives you this fancy API but under the covers, it calls poll in a loop and only invokes your method when records have been retrieved.
You can easily build something similar with any of the Python clients you've mentioned. Like in Java, this is not an API directly exposed by (most) Kafka clients but instead something provided by a layer on top. It's something you need to build.
Here is an implementation of the idea given by @MickaelMaison's answer. I used kafka-python.
The polling is done in a different thread. Once a message is received, the listener is called by passing the data retrieved from Kafka.