Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Running Python Greenlets in WebAssembly - Wasme...

Running Python Greenlets in WebAssembly - Wasmer - Wasm I/O 2026

Python greenlets have been a long-standing blocker for running real Python backends on WebAssembly. In this talk, we’ll show how we were able to support Python greenlets natively with WebAssembly unlocking frameworks like SQLAlchemy on the edge and in the browser, without rewrites or async refactors.

Avatar for Syrus Akbary

Syrus Akbary

March 19, 2026
Tweet

More Decks by Syrus Akbary

Other Decks in Programming

Transcript

  1. What is SQLAlchemy? • It’s a popular DB ORM and

    toolkit for Python (320 million downloads/month) • It relies on greenlets (Green threads) heavily to not having work being blocked by DB I/O
  2. Python Greenlet’s • It was the async abstraction library of

    Python before it had “async” “await” syntax • It allowed having non-blocking IO functions • It’s a stackful coroutine
  3. Stackless coroutines continuations, generators, async/ await Have to be suspended

    from a “awaitable” function. Normally they are “embedded” into the language
  4. Coroutines in Wasm • Stackless coroutines are trivial to run

    and in the generated artifacts (Wasm) • Stackful coroutines require cooperation with the JS/Wasm runtime (or operating with assembly directly)
  5. Making Stackful Coroutines run in the browser There are di

    ff erent ways to make them work: • Asyncify • Wasm Stack Switching Proposal • JS Promise Integration (JSPI)
  6. Asyncify Brings Pause and Resume to WebAssembly programs Could bring

    “emulated” support for Stackful Coroutines. $ wasm-opt input.wasm -O1 --asyncify -o output.wasm output.wasm 1.5 bigger 2x slower
  7. Wasm Stack Switching Proposal The Stack Switching proposal introduces a

    small set of new Wasm instructions: • cont.new : creates a new continuation that can yield execution to others • cont.bind: is a monad for continuations (it returns a new function that calls the continuation with extra arguments) • suspend : it suspends the current running continuation • resume*, switch, : it invokes a suspended continuation
  8. JS Promise Integration (JSPI) The JS Promise Integration proposal: •

    Had a simpler surface than the Wasm Stack Switching proposal: • WebAssembly.Suspending allows importing async functions • WebAssembly.promising allows calling wasm functions that can yield • It’s already enabled in the browsers: • Chrome has it live • Safari has it on their last preview • Firefox is implementing it
  9. We mirrored the JS Promise Integration proposal in the Wasmer

    Rust API, using native async syntax and without explicitly tying execution to any scheduler New Async Function API in Wasmer
  10. We added a Context-switching API in WASIX To complete the

    design, we exposed explicit context-switch operations so WASIX programs can manage continuations directly.