I am trying to understand potential slowdown in my FastAPI app.
My understanding is that each time I do an await, a task is scheduled for later, creating a backlog of task to be executed. If the process is slow for some reason, the number of pending tasks should raise. How can I monitor the number of pending tasks?
You could do that using
asyncio.all_tasks(loop=None):As described in this article (which I would suggest having a look at):
Moreover, as explained in this related article, one could monitor and get details about all running tasks, in order to detect any stuck long-running tasks in the
asyncioevent loop, as follows:which would return something like:
Further, one could report the full stack trace of each task:
Finally, your understanding about
async/await(coroutines) in asynchronous programming is not entirely correct, and thus, I would suggest having a look at this answer for more details.