Closed scheung38 closed 1 year ago
I'm also interested to know what the recommended way of doing this is.
My use case is slightly different as I want to use Kafka. I was planning to use the example found in this blog post from Confluent that was suggesting to use FastAPI events to start/stop the producer:
@app.on_event("startup")
async def startup_event():
global producer
producer = await create_producer()
@app.on_event("shutdown")
def shutdown_event():
producer.close()
To avoid dropping messages, requests should not be processed before the producer is ready. This can be taken into account when implementing create_producer
. Also, it's probably a good idea to also add the producer to the application health check. In my case, I would use Kubernetes so the health check could be used as liveness and readiness probes.
I don't think FastAPI would need to be involved when creating consumers. To validate the content of the messages, Pydantic could be used directly.
Hey @scheung38 I have an example here: https://github.com/toh995/fastapi-faust-example
The README for the repo has a high level explanation of the architecture.
Hope it helps :)
Thanks for the discussion everyone! 🍰
I'm not using Faust myself, but I imagine something like @NicolasTr's example or @toh995's example should work. 🤓
Sorry for the long delay! 🙈 I wanted to personally address each issue/PR and they piled up through time, but now I'm checking each one in order.
Assuming the original need was handled, this will be automatically closed now. But feel free to add more comments or create new issues or PRs.
if so could someone provide Hello World real time example