How to get async functionality to work? #7765
Replies: 6 comments
-
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.0
Add 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
<#958?email_source=notifications&email_token=AAINSPSATB5ARVUEQX6FS53RBW47RA5CNFSM4KRUD3G2YY3PNVWWK3TUL52HS4DFUVEXG43VMWVGG33NNVSW45C7NFSM4IL5B24Q>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AAINSPTIZXCHRZ6Q3GOYQCDRBW47RANCNFSM4KRUD3GQ>
.
|
Beta Was this translation helpful? Give feedback.
-
@euri10 - 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(),
} |
Beta Was this translation helpful? Give feedback.
-
This is a "how to use asyncio in Python" and is unrelated to FastAPI. The You will want to use Note: If you are used to Javascript async/await or promises, the Python asyncio machinery does not function in quite the same manner. |
Beta Was this translation helpful? Give feedback.
-
Thanks for the help here everyone! Yep. You can user HTTPX's async tools for that. |
Beta Was this translation helpful? Give feedback.
-
Assuming the original issue was solved, it will be automatically closed now. But feel free to add more comments or create new issues. |
Beta Was this translation helpful? Give feedback.
-
First check
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.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.0
Add any other context or screenshots about the feature request here.
Beta Was this translation helpful? Give feedback.
All reactions