Search⌘ K
AI Features

... continued

Explore running event loops with Python's asyncio module, including asyncio.run() for Python 3.7+, managing multiple event loops, and scheduling callbacks. Understand how blocking affects the event loop and how to handle asynchronous tasks properly across different Python versions.

We'll cover the following...

Running the Event Loop

With Python 3.7+ the preferred way to run the event loop is to use the asyncio.run() method. The method is a blocking call till the passed-in coroutine finishes. A sample program appears below:

async def do_something_important():
    await asyncio.sleep(10)


if __name__ == "__main__":

  asyncio.run(do_something_important())

If you are working with Python 3.5, then the asyncio.run() API isn't available. In that case, we explicitly retrieve the event loop using asyncio.new_event_loop() and run our desired coroutine using run_until_complete() defined on the loop object. The code widget below ...