Fastapi: [Question] Can I use FastAPI with Faust for Python based streaming apps?

Created on 11 Jul 2020  路  2Comments  路  Source: tiangolo/fastapi

if so could someone provide Hello World real time example

question

Most helpful comment

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 :)

All 2 comments

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 :)

Was this page helpful?
0 / 5 - 0 ratings