fastapi / fastapi

FastAPI framework, high performance, easy to learn, fast to code, ready for production
https://fastapi.tiangolo.com/
MIT License
75.86k stars 6.4k forks source link

Can I use FastAPI with Faust for Python based streaming apps? #1705

Closed scheung38 closed 1 year ago

scheung38 commented 4 years ago

if so could someone provide Hello World real time example

NicolasTr commented 4 years 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.

toh995 commented 3 years ago

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

tiangolo commented 1 year ago

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.

github-actions[bot] commented 1 year ago

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.