I have a Flask API, which when hit by a client, will subscribe to a Kafka topic and start consuming (polling) data from the topic and write it to a database.
Currently the consumer function runs an infinite loop to poll the data and write it to the database. But the problem here is, the client doesn't receive a success response here. The further client processes happens after the server timeout.
But the objective is, client has to receive the response right after the consumer is initialized and is ready to consume.
How to implement it in a proper way?
Edit:
I came across this thread - Flask end response and continue processing
Seems that using Python's Thread Library won't work here according to the comments on the top voted answer.
And the Accepted Answer suggested to use WSGI middleware that adds a hook to the close method of the response iterator. But is it advisable in this case where the consumer will be running for an infinite amount of time?
Is there an alternative to these?