... continued
In this lesson, we'll examine some of important APIs we can use to work with the event loop.
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 ...