Skip to content

Commit b574f72

Browse files
committed
add section on asyncio.run
1 parent 1ed21c0 commit b574f72

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

Doc/howto/a-conceptual-overview-of-asyncio.rst

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,22 @@ to specify the event loop.
173173
# This creates a Task object and puts it on the event loop's queue.
174174
task = asyncio.create_task(coro=loudmouth_penguin(magic_number=5))
175175

176+
Earlier, we manually created the event loop and set it to run forever.
177+
In practice, it's recommended to use (and common to see) :func:`asyncio.run`,
178+
which takes care of managing the event loop and ensuring the provided
179+
coroutine finishes before advancing.
180+
For example, many async programs follow this setup::
176181

182+
import asyncio
183+
184+
async def main():
185+
...
186+
187+
if __name__ == "__main__":
188+
asyncio.run(main())
189+
# The program will not reach the following print statement until the
190+
# coroutine main() finishes.
191+
print("coroutine main() is done!")
177192

178193
=====
179194
await

0 commit comments

Comments
 (0)