Skip to content

Run your Python in Web Workers: a small kit - #10

Merged
bdbarnett merged 1 commit into
mainfrom
ostinato/worker-kit
Oct 15, 2026
Merged

bdbarnett merged 1 commit into
mainfrom
ostinato/worker-kit

Conversation

@bdbarnett

Copy link
Copy Markdown
Contributor

When an app redraws a lot and also has to react on time (Web MIDI, live audio, fast input), a long redraw on the page's main thread delays everything else. workers/ lets the app's Python run in Web Workers while the page stays thin:

  • workers/host.js (the page): starts workers, hands a canvas to one of them as an OffscreenCanvas and relays its pointer, wheel and key events, relays Web MIDI to the workers that ask, and owns the AudioContext.
  • workers/python_worker.js (each worker): loads the vendored Pyodide, installs your wheels, copies your files in, runs your module.
  • workers/audio_worklet.js (the audio thread): plays the 256-frame blocks an audio worker sends, with a queue that starts at 1,536 frames, works down to 768 when the machine keeps up and backs off after any dropout.

On the Python side it pairs with pydevices' displaydev.workerdisplay (with board_configs/workerdisplay), audiodev.worklet_audio and midiports.webmidi.relay_ports. docs/workers.md shows the whole setup. Nothing about the default single-thread template changes.

workers/host.js starts Pyodide workers, hands a canvas to one and relays its
input, relays Web MIDI, and owns the AudioContext; workers/python_worker.js
boots Pyodide in each worker; workers/audio_worklet.js plays what an audio
worker sends, with a queue that adapts to the machine. docs/workers.md shows
how to use it.
@bdbarnett
bdbarnett merged commit cb07937 into main Oct 15, 2026
1 check passed
@bdbarnett
bdbarnett deleted the ostinato/worker-kit branch October 15, 2026 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant