The Event Loop pattern is at the core of asynchronous programming. This short series shows how to implement an event loop (from scratch!) in just about 100 lines of pure Python and how to add support of async/await
in 200 more lines using generators. Say no to callback hell!
Asynchronous Programming Basics
Learning Series
Explaining event loop in 100 lines of code
There is plenty of articles out there about the event loop. However, as a software engineer, I prefer to read code, not text. And there is no better way of learning a new concept than implementing it yourself. So, let's try to grasp the idea of the event loop by coding a new and shiny one.
In the article, I'll try to describe the idea of the event loop in general, not a specific implementation of the event loop in Node.js or Python, or some other language/library.
Explaining async/await in 200 lines of code
In the previous article, we learned how to implement a simple but workable event loop. However, programs which are supposed to be run by the event loop are full of callbacks. This is the usual problem of event-loop-driven environments. When business logic becomes reasonably complicated, callbacks make program's code hardly readable and painfully maintainable. And the callback hell begins! There is plenty of ways to deal with the artificial complexity arose due to callbacks, but the most impressive one is to make the code great flat again. And by flat, I mean callback-less and synchronous-like. Usually, it's done by introducing async/await
syntactic feature. But every high-level abstraction is built on top of some basic and fundamental ideas. Let's check the async/await
sugar out and see what exactly happens under the hood.
Don't miss new posts in the series! Subscribe to the blog updates and get deep technical write-ups on Cloud Native topics direct into your inbox.