Introducing event loops

The most important concept within asyncio is the event loop. An event loop allow you to write asynchronous code using either callbacks or coroutines.

The keys to understanding asyncio are the terms of coroutines and the event loop. Coroutines are stateful functions whose execution can be stopped while another I/O operation is being executed. An event loop is used to orchestrate the execution of the coroutines.

To run any coroutine function, we need to get an event loop. We can do this with loop = asyncio.get_event_loop().

This gives us a BaseEventLoop object. This has a run_until_complete method that takes in a coroutine and runs it until completion. Then, the coroutine returns a result. At a low level, an event loop executes the BaseEventLoop.rununtilcomplete(future) method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset