Another weekend, another weekend read, this time all about Event Loops
My book Thinking in Distributed Systems is now available. 12 chapters, full of mental models, diagrams, illustrations, and examples, all about distributed systems.
In his blog post A Tale of Event Loops, André Caron explores the fundamentals of python’s asyncio. Central to André’s exploration is the concept of coroutines and event loops.
Unlike traditional functions that execute to completion upon invocation, a coroutine’s execution can be resumed and suspended: Invoking a coroutine function returns a coroutine object that can be resumed (control is transferred from the caller to the coroutine) and that can suspend (control is transferred from the coroutine to the caller).
>>> async def hello(name):
... print('Hello, %s!' % (name,))
>>>
>>> coro = hello('world')
>>> type(coro)
<class 'coroutine'>
Having a reference to a computation that can be resumed and suspended allows the caller to have a “conversation” with the computation-the very essence of an event loop.
In effect, an event loop is scheduler that manages multiple coroutines. Coroutines yield to the event loop, often with a request such as reading data from a socket and will be resumed with a response such as the data once the event loop has processed the request.
>>> # request that the event loop tell us when we can read.
>>> @coroutine
... def recv(stream, size):
... yield (EVENT_READ, stream)
... return stream.recv(size)
This way we can write sequential looking code that gets executed concurrently either on one thread or multiple threads.
This blog post not only sheds light on the mechanics of asynchronous programming but also encourages readers to experiment and build their own event loops.
Happy Reading
A Tale of Event Loops
André Caron
This is my attempt at trying to capture the core fundamentals on which [asyncio] is based. Hopefully, it will be a good stepping stone to get acquainted with [asyncio]’s source code.
Let's get started :-)