How can I properly utilize the asynchronous within a route? The following code takes 4 seconds to complete a call to /async route, while I expect it to only take 2 seconds.
import time
from fastapi import FastAPI
app = FastAPI()
async def get_a():
time.sleep(2)
return 'a'
async def get_b():
time.sleep(2)
return 'b'
@app.get("/async")
async def asynchronous():
a = await get_a()
b = await get_b()
return {
'a': a,
'b': b,
}
Environment is running off docker container tiangolo/uvicorn-gunicorn-fastapi:python3.7
App was was started with uvicorn app.app:app --reload --host 0.0.0.0
Add any other context or screenshots about the feature request here.
time.sleep blocks
Use asyncio.sleep
Le ven. 7 févr. 2020 à 9:03 PM, Scott Cronin notifications@github.com a
écrit :
First check
- I used the GitHub search to find a similar issue and didn't find it.
- I searched the FastAPI documentation, with the integrated search.
- I already searched in Google "How to X in FastAPI" and didn't find
any information.Description
How can I properly utilize the asynchronous within a route? The following
code takes 4 seconds to complete a call to /async route, while I expect
it to only take 2 seconds.import timefrom fastapi import FastAPI
app = FastAPI()
async def get_a():
time.sleep(2)
return 'a'
async def get_b():
time.sleep(2)
return 'b'
@app.get("/async")async def asynchronous():
a = await get_a()
b = await get_b()
return {
'a': a,
'b': b,
}Additional context
Environment is running off docker container
tiangolo/uvicorn-gunicorn-fastapi:python3.7
App was was started with uvicorn app.app:app --reload --host 0.0.0.0Add any other context or screenshots about the feature request here.
—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/tiangolo/fastapi/issues/958?email_source=notifications&email_token=AAINSPSATB5ARVUEQX6FS53RBW47RA5CNFSM4KRUD3G2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IL5B24Q,
or unsubscribe
https://github.com/notifications/unsubscribe-auth/AAINSPTIZXCHRZ6Q3GOYQCDRBW47RANCNFSM4KRUD3GQ
.
@euri10 - asyncio.sleep did work.
Instead of writing a dummy function like
async def get_b():
asyncio.sleep(2)
return 'b'
How would I write a function that makes requests like the following. Note: this request takes 2x as long as I expect, where each request takes about the same amount of time. I have tried with both requests and httpx libraries.
async def get_taxonomy():
return httpx.post(TAXONOMY_ENDPOINT, json=TAXONOMY_PAYLOAD).json()
async def get_attributes():
return httpx.post(ATTRIBUTE_ENDPOINT, json=ATTRIBUTE_PAYLOAD).json()
@app.get("/async")
async def asynchronous():
return {
'taxonomy': await get_taxonomy(),
'attributes': await get_attributes(),
}
This is a "how to use asyncio in Python" and is unrelated to FastAPI.
The await keyword blocks the execution of the remaining code in the function, so each await causes the code in that function to execute synchronously (await causes the event loop to execute the next event scheduled on the event loop).
You will want to use asyncio.create_task() or asyncio.gather() to schedule and execute coroutines on the event loop. This will allow both httpx methods to make their requests and wait for their responses in an asynchronous manner.
Note: If you are used to Javascript async/await or promises, the Python asyncio machinery does not function in quite the same manner.
Thanks for the help here everyone!
Yep. You can user HTTPX's async tools for that.
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues.
Most helpful comment
@euri10 -
asyncio.sleepdid work.Instead of writing a dummy function like
How would I write a function that makes requests like the following. Note: this request takes 2x as long as I expect, where each request takes about the same amount of time. I have tried with both requests and httpx libraries.