Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

await vs asyncio.run() in Python

In Python, what is the actual difference between awaiting a coroutine and using asyncio.run()? They both seem to run a coroutine, the only difference that I can see being that await can only be used in a coroutine.

like image 922
R Z Avatar asked Nov 22 '20 21:11

R Z


1 Answers

That is the exact difference. There should be exactly one call to asyncio.run() in your code, which will block until all coroutines have finished.

Inside any coroutine, you can use await to suspend the current function, and asyncio will resume the function at some future time. All of this happens inside the asyncio.run() function, which schedules what functions can run when.

like image 55
mousetail Avatar answered Sep 23 '22 05:09

mousetail