Notes on structured concurrency, or: Go statement considered harmful
3 hours ago
- #concurrency
- #software-design
- #programming-languages
- Concurrency APIs have traditionally used go statements (thread spawning, callbacks, futures) that allow background tasks to run independently, similar to goto statements in control flow.
- Go statements break abstraction by making functions non-black-box (they may spawn tasks without caller knowledge), disrupt automatic resource cleanup, and hinder proper error propagation.
- Trio introduces nurseries as a structured alternative, where tasks are started within a nursery block and the parent task waits for all child tasks to finish before exiting, ensuring control flow follows the 'black box' rule.
- Nurseries preserve function abstraction, support dynamic task spawning, allow explicit passing of nursery objects for flexibility, enable custom nursery-like types, and integrate automatic error propagation and cancellation.
- Removing go statements enables new features like reliable cancellation and proper control-C handling in Python, and nurseries encourage clearer, more robust concurrent programming, as demonstrated by simplified implementations like Happy Eyeballs in Trio.