How can I properly utilize the asynchronous functionality in a FastAPI route?
The following code snippet takes 10 seconds to complete a call to my /home
route, while I expect it to only take 5 seconds.
from fastapi import FastAPI
import time
app = FastAPI()
async def my_func_1():
"""
my func 1
"""
print('Func1 started..!!')
time.sleep(5)
print('Func1 ended..!!')
return 'a..!!'
async def my_func_2():
"""
my func 2
"""
print('Func2 started..!!')
time.sleep(5)
print('Func2 ended..!!')
return 'b..!!'
@app.get("/home")
async def root():
"""
my home route
"""
start = time.time()
a = await my_func_1()
b = await my_func_2()
end = time.time()
print('It took {} seconds to finish execution.'.format(round(end-start)))
return {
'a': a,
'b': b
}
I am getting the following result, which looks non asynchronous:
λ uvicorn fapi_test:app --reload
INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO: Started reloader process [5116]
INFO: Started server process [7780]
INFO: Waiting for application startup.
INFO: Application startup complete.
INFO: 127.0.0.1:51862 - "GET / HTTP/1.1" 404
Func1 started..!!
Func1 ended..!!
Func2 started..!!
Func2 ended..!!
It took 10 seconds to finish execution.
INFO: 127.0.0.1:51868 - "GET /home HTTP/1.1" 200
But, I am expecting FastAPI to print like below:
Func1 started..!!
Func2 started..!!
Func1 ended..!!
Func2 ended..!!
It took 5 seconds to finish execution.
Please correct me if I am doing anything wrong?