I have used async
keyword in all of the apis in FastAPI python. While I am running the application parallelly in two tabs in chrome in localhost
, when i open a component in one tab it usually takes around 3 to 4 sec for loading the api and in the meantime when i click the other components in the next tab, the api call in the tab is underwaiting till the first tab api gets loaded.
Could any one please help me solve this?
Asked
Active
Viewed 26 times
0
-
1`async` does not magically make things run in parallel; it just gives out time to other code when you call `await`. If you aren't using async compatible interfaces inside your endpoints in FastAPI (i.e. to retrieve the comments), the server will be stuck waiting for that piece of code to finish. FastAPI will run your controller endpoints in a threadpool if you _don't mark them as async_, which might help. However, if you want to go the async route, _make sure that anything depending on io_ uses `await` and a async compatible interface. – MatsLindh Apr 18 '23 at 11:16
-
@Chris yes but when i use asyncio.sleep() no changes still the api is waiting . these api calls are made from frontend – Ramyaa R Apr 18 '23 at 11:33
-
Just adding `asyncio.sleep()` does do anything magical. Your underlying code that gets called from the controller endpoints needs to be async compatible and awaited. Adding sleep will just make everything take longer, but at least that wait-time won't block other code from running. – MatsLindh Apr 18 '23 at 11:41