Skip to content

Try “polymorphism”, “TCP handshake” or “git rebase”.

    Computer Basics / Operating Systems

    Waiting for I/O: Blocking, Non-Blocking and Event Loops

    I/O means input/output: any reading or writing to disk, network, keyboard, etc.

    Chapter 11 of 124 min read

    The Plain Picture#

    You order food at a busy counter. Two styles of restaurant:

    • Style 1 — stand and wait. You stand at the counter until your food is ready. The cashier can't serve anyone else while you wait. To serve 100 customers at once, the restaurant needs 100 cashiers.

    • Style 2 — take a buzzer. You order, get a buzzer, and sit down. The single cashier serves the next customer. When your food is ready, your buzzer goes off and you collect it. One cashier serves hundreds of people, because nobody is standing around waiting.

    The Real Names#

    • I/O means input/output: any reading or writing to disk, network, keyboard, etc.

    • Blocking I/O (style 1): the thread calls read and is put to sleep (BLOCKED, Ch 3) until data arrives. Simple to write, but each waiting request ties up a whole thread.

    • Non-blocking I/O (style 2): the call returns immediately with "not ready yet", and the program does other work.

    • I/O multiplexing (the buzzer system): one thread asks the kernel "tell me when any of these thousand connections has data". On Linux this system call is epoll; on macOS it's kqueue.

    • An event loop is a program built around this: wait for events, run the small piece of code handling each one, go back to waiting.

    Under the Hood#

    A web server handling 1,000 users, each waiting ~200 ms for the database:

      THREAD-PER-REQUEST (blocking)                EVENT LOOP (non-blocking)
      ────────────────────────────                 ──────────────────────────
      thread 1 ── waiting on DB ─────── done       one thread:
      thread 2 ── waiting on DB ─────── done         send query 1   (don't wait)
      thread 3 ── waiting on DB ─────── done         send query 2   (don't wait)
        ...                                           ...
      thread 1000 ─ waiting on DB ───── done         send query 1000
                                                     epoll: "which queries replied?"
      1,000 threads, each with its own stack,        → handle replies as they arrive
      mostly sleeping                                1 thread, very little memory
    • Thread-per-request is easier to write and fine for moderate load.

    • The event loop scales to far more connections per machine for I/O-bound work (Ch 4). It's how Node.js, nginx, and Python's asyncio (which FastAPI uses) work.

    The catch: in an event loop, if the code for one event does heavy CPU work (or a blocking call) for 2 seconds, every other user waits 2 seconds — the single cashier is stuck. This is the #1 mistake in async Python and Node.js, and you'll study it properly in Python Concurrency (coming soon) and Asynchronous JavaScript and the Event Loop (coming soon).

    Gotchas

    • Async doesn't make CPU work faster. It only helps with waiting. CPU-bound work needs more cores: multiple processes or threads.

    • One blocking call in async code blocks everyone on that event loop.

    Check yourself

    4 questions
    1

    What's the difference between blocking and non-blocking I/O?

    Show answer

    Blocking puts the thread to sleep until data arrives; non-blocking returns immediately so the program can do other work.

    2

    What do epoll and kqueue do?

    Show answer

    Let one thread wait on many connections at once and get told which ones are ready (Linux and macOS versions).

    3

    Why can one event-loop thread serve thousands of connections?

    Show answer

    Most of the time connections are waiting, and waiting costs nothing — the thread only works when data is ready.

    4

    What happens if a request handler in an event loop does 2 seconds of heavy calculation?

    Show answer

    Every other request on that loop is stuck for those 2 seconds.