1

I am creating a FastAPI in which calling requests.Session post method for a localhost URL in a function as below.

from uvicorn import run as uvicorn_run
from fastapi import FastAPI, Request
from requests import Session

app = FastAPI()
session = Session()

async def get_text(text):
    r = session.post('http://localhost:5005/webhooks/rest/webhook', 
                         json ={'message':text})

    if r.status_code == 200:
        response = await r.json()[0]
    else:
        response = None

    return response

@app.post('/')
async def main(info: Request):
    text = await info.json()
    message = text.get('message')
    response = await get_text(message)
    return response

if __name__ == '__main__':
    uvicorn_run("file_name:app", host="0.0.0.0", port=8080, reload=True, workers=5)

When calling the API, it is taking more than 2 seconds to respond every time I hit. When I'm running the get_text function separately in a Jupyter Notebook without API, only for the first run it is taking 2 seconds to execute, rest of the calls it is taking just milliseconds. I want to speed up the request.Session call within the API.

Please let me know how can I achieve it. Any code improvement suggestions are also welcome.

Abdul Quddus
  • 111
  • 2
  • 10
  • Please have a look at this [related answer](https://stackoverflow.com/a/73770074/17865804) as well. – Chris Oct 12 '22 at 05:06
  • 1
    @Chris Thanks a lot for navigating to the proper answer!! I have implemented the solution you have mentioned and now getting API responses in milliseconds. Thanks once again – Abdul Quddus Oct 12 '22 at 07:34
  • You need to post a [mre] and debugging details. Requests library does not provide async API. The code, ```await r.json()[0]``` will not run. – relent95 Oct 12 '22 at 07:38

0 Answers0