liveBook · Manning

Event notification system

As explained in Python Concurrency with asyncio

An operating-system mechanism that tracks asynchronous I/O resources and reports when they are ready for processing.

An event notification system lets the operating system track non-blocking sockets and notify an application when they are ready. Instead of blocking while waiting for bytes, the Python thread can run other code and resume the waiting task when I/O completes. section 1.6.1

The mechanism supports asyncio-style concurrency through a simple sequence:

  • Hand a non-blocking I/O operation to the operating system. section 1.6.1
  • Continue executing other Python code while the operating system tracks socket readiness. section 1.6.1
  • Wake the task and process its result when the operation finishes. section 1.6.1

Operating systems provide different implementations: kqueue on FreeBSD and macOS, epoll on Linux, and IOCP on Windows. These mechanisms avoid the costly polling loop that repeatedly tries operations and catches BlockingIOError. section 1.6.1 section 3.3

A non-blocking server

A socket configured with setblocking(False) can accept connections without waiting. A hand-built loop that repeatedly calls accept and recv must catch unavailable-I/O errors, consuming nearly 100% CPU; event notification replaces that busy polling with readiness notifications. section 3.3

Figure 1.8 Making a non-blocking I/O request returns immediately and tells the O/S to watch sockets for data. This allows execute_other_code() to run right away instead of waiting for the I/O requests to finish. Later, we can be alerted when I/O is complete and process the response.
01-08

Trace how a non-blocking I/O request returns immediately, lets other code run, and later signals that socket data is ready. section 1.6.1

How Python Concurrency with asyncio covers it

How deep it goes
Explains non-blocking handoff, task wake-up, platform variants, and polling costs, but stops short of implementation internals.
Examples
Uses non-blocking socket servers, readiness-flow diagrams, exception traces, and CPU-utilization comparisons.
What it assumes
Assumes basic socket operations and introduces non-blocking I/O, operating-system handoff, and readiness notification progressively.
What you can do after
Understand why event notification enables asyncio concurrency and diagnose why exception-driven polling wastes CPU.
How it connects
Connects non-blocking sockets, asyncio, event loops, concurrency, polling, and BlockingIOError.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Python Concurrency with asyncio. The section links point to the parts of the book it draws on.