HN
Today

Async Python Is Secretly Deterministic

The article unravels a "secret" of Async Python: its asyncio event loop schedules tasks deterministically, specifically the initial order of tasks created by asyncio.gather. This surprising behavior enables robust durable workflows and replay-based recovery by ensuring a consistent step order. However, commenters swiftly debate whether relying on such an implementation detail is wise or warranted, questioning its specification and long-term stability.

11
Score
6
Comments
#9
Highest Rank
5h
on Front Page
First Seen
Apr 3, 7:00 PM
Last Seen
Apr 3, 11:00 PM
Rank Over Time
169121216

The Lowdown

The article from DBOS explores how their team leveraged a nuanced aspect of Python's asyncio event loop to build deterministic durable workflows, which are crucial for replay-based recovery. It delves into the internal workings of asyncio to reveal a "secret" determinism that, at first glance, might seem counterintuitive for concurrent programming.

  • Durable execution systems require deterministic workflows for replay-based recovery, which traditionally poses a challenge with concurrent async operations where task completion order can vary.
  • While asyncio.gather is commonly used for running concurrent tasks, the exact interleaving of their execution can be non-obvious and seemingly non-deterministic.
  • At its core, asyncio operates on a single-threaded event loop that uses a scheduler to process a queue of tasks.
  • Crucially, when new tasks are created and scheduled (e.g., via asyncio.gather), the event loop processes them in a strict First-In, First-Out (FIFO) order.
  • Although the completion order and subsequent interleaving of these tasks can be unpredictable, the initial order in which they begin execution is deterministic.
  • DBOS exploits this by assigning unique step IDs before the first await call within a task, ensuring that workflow steps are processed in a consistent, predictable order.
  • This deep understanding of the asyncio event loop allows for writing concurrent code that is both deterministic and inherently safer, simplifying the reasoning behind complex async systems.

The author concludes that appreciating these subtleties of the asyncio event loop empowers developers to create more reliable and predictable systems, inviting readers to explore DBOS's tools for durable workflows.

The Gossip

Deterministic Doubts & Details

Users debate the prudence of building critical systems on an undocumented or unspecified behavior of `asyncio`. Some argue that relying on such an implementation detail is a brittle foundation, citing other async frameworks (like Trio) that actively randomize task order to prevent developers from accidentally depending on it. Conversely, others acknowledge the significant benefits of determinism for debugging and ensuring reproducible behavior in complex systems.

I/O's Interruption & Irregularity

A key concern raised is whether this "secret determinism" holds up when real-world factors like network calls or file I/O operations are introduced. Commenters question if the article's claims of determinism can withstand the inherent non-deterministic nature of external I/O. The author clarifies that while tasks *start* in a deterministic order, their subsequent interleaving and *completion* order are indeed influenced by I/O and are not guaranteed to be identical across runs.