Find answers from the community

Updated 2 months ago

Anyone have any information about the

Anyone have any information about the speed differences between Flask and FastAPI? We were thinking about switching from Flask to FastAPI but it seems to be quite a bit slower. A basic request where I use the chat engine seems to be almost twice as slow...
M
L
k
21 comments
We're using Gunicorn to run the app.
Are you using async in fastapi? Everything in fastapi should be using async methods when possible
I believe we are, and if we were to do just one request, it shouldn't matter right?
Bit more background here, when we used astream_chat and async_response_gen the streaming didn't return anything at all.
We're seeing this even when just a single request is send by 1 person at a time.
I havent compared performance between fast-api and flask myself πŸ€” But imo the main advantage of fastapi is typed interface validation and automatic swagger docs
Also for reference, here's a small sample app with astream_chat (this is probably not a good architecture, but just trying to show how it works)
Plain Text
from fastapi import FastAPI
from fastapi.responses import StreamingResponse

from llama_index import SimpleDirectoryReader, VectorStoreIndex


documents = SimpleDirectoryReader("./docs/examples/data/paul_graham/").load_data()
index = VectorStoreIndex.from_documents(documents)
agent = index.as_chat_engine()

app = FastAPI()
app.agent = agent


@app.get("/chat/")
async def chat(message: str) -> StreamingResponse:
    response = await app.agent.astream_chat(message)

    response_gen = response.async_response_gen()

    return StreamingResponse(response_gen, media_type="text/plain")


@app.get("/")
async def root():
    return {"message": "Hello World"}


if __name__ == "__main__":
    import uvicorn

    uvicorn.run(app, loop="asyncio", host="127.0.0.1", port=8000)
curl -N http://127.0.0.1:8000/chat/?message=Hello
(tbh it feels very fast to me, but this is also a pretty simple setup)
@Logan M will try this after dinner πŸ™‚ stay tuned.
@Logan M just tried it and it seems to be a bit faster but now after 3 messages I run into RuntimeError: Event loop is closed errors πŸ˜„
Hmmm usually the event loop closes because of some error earlier on... it's like a swallower of errors πŸ˜…
I wonder if it was a request timeout maybe?
I'll ask a teammate who I know uses fastapi a lot
(Using a global like that is also not a great idea lol)
I added nest_asyncio.apply() and that seems to have fixed it.
is that recommended? πŸ˜„ or did i know just surpress an error lol
That's definitely recommended! 🫑
That's why you have to set the loop type to asyncio πŸ‘
awesome, all seems to run smooth so far. @Logan M thanks for your support.
Add a reply
Sign up and join the conversation on Discord