Session
From Generators to Event Loops: Understanding Python Async Internals
From Generators to Event Loops: Understanding Python Async Internals
Duration: 25 minutes
Audience Level: Intermediate Python developers
Prerequisites:Familiarity with Python functions, iterators, and basic networking concepts.
Talk Overview:
Python's `async` and `await` keywords have become fundamental tools for building scalable applications, yet many developers use them without understanding the machinery operating underneath. This talk aims to remove the mystery around Python's asynchronous programming model by rebuilding the core ideas behind `asyncio` from scratch using only the Python standard library.
Rather than treating asyncio as a black box, attendees will progressively construct a miniature version of Python's async runtime, gaining a practical understanding of event loops, coroutine scheduling, non-blocking I/O, tasks, and futures.
The session combines live coding, visual explanations, and architectural walkthroughs to connect high-level async code with the low-level mechanisms that make it work.
Detailed Outline:
1. The Problem with Blocking I/O (3 minutes)
* Demonstrate a simple blocking socket operation.
* Show how a single blocking call prevents other work from progressing.
* Briefly discuss traditional concurrency approaches such as threads and processes.
* Introduce cooperative multitasking and event-driven programming as an alternative.
Key takeaway: Concurrency problems often stem from waiting on I/O rather than CPU limitations.
2. Generators: The Hidden Foundation of Async (4 minutes)
* Review Python generators and the `yield` statement.
* Demonstrate pausing and resuming execution.
* Build a minimal scheduler capable of interleaving multiple generator-based tasks.
* Explain how coroutines evolved from generator mechanics.
Key takeaway: The foundations of async programming already exist within Python's generator model.
3. Building an Event Loop from Scratch (6 minutes)
* Introduce the `selectors` module from the standard library.
* Explain readiness-based I/O.
* Build a minimal event loop that:
* Tracks pending operations
* Waits for I/O readiness
* Resumes suspended coroutines
* Visualize how scheduling occurs.
Key takeaway: The event loop is the central coordinator of asynchronous execution.
4. Tasks, Futures, and Scheduling (5 minutes)
* Create lightweight task wrappers around coroutines.
* Implement simple future-like objects.
* Build scheduling logic similar to:
* `asyncio.run()`
* `asyncio.create_task()`
* Demonstrate task lifecycle management.
Key takeaway: Asyncio's higher-level abstractions are built on relatively simple primitives.
5. Async/Await Syntax Demystified (3 minutes)
* Examine how Python compiles `async def`.
* Use the `inspect` and `dis` modules to explore generated bytecode.
* Show the relationship between:
* Generators
* Coroutines
* `await`
* Connect language syntax back to the runtime built earlier.
Key takeaway: `await` is elegant syntax built on understandable underlying concepts.
6. Building a Real Async HTTP Proxy (3 minutes)
* Demonstrate a working asynchronous HTTP proxy powered by the custom event loop.
* Handle multiple concurrent client connections.
* Compare behavior against the earlier blocking implementation.
Key takeaway: The concepts discussed throughout the talk can power real-world applications.
7. Closing Thoughts and Modern Context (1 minute)
* Connect the implementation to modern frameworks and libraries.
* Discuss where concepts such as structured concurrency and production event loops extend the ideas presented.
* Highlight practical benefits of understanding async internals.
Learning Outcomes:
By the end of the session, attendees will:
* Understand how Python implements asynchronous programming.
* Explain the relationship between generators, coroutines, and async/await.
* Understand the role of the event loop in coordinating concurrency.
* Develop an accurate mental model of tasks and scheduling.
* Be better equipped to debug, optimize, and reason about asynchronous applications.
Muhammed Mizaj
Product Engineer at UST Global
Thiruvananthapuram, India
Links
Please note that Sessionize is not responsible for the accuracy or validity of the data provided by speakers. If you suspect this profile to be fake or spam, please let us know.
Jump to top