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.