Threads, Frames and Propagation

A pipeline is a set of nodes and the connections between them. This page is about what runs where once p.start() has returned, and in what order a frame meets the nodes.

Sources run on threads of their own

Every live source acquires on a thread the pipeline does not own: a Generator on a timer, a BCI Core on a worker thread fed by the Bluetooth driver’s callback, a GDS amplifier on its driver’s callback, a Unicorn on a reader thread that polls the device, an LslReceiver on a thread that pulls from the inlet. That is why p.start() returns at once and the script goes on to app.run(): nothing in the pipeline waits for the main thread.

A frame is carried through the chain in one cycle

When a source has a full frame, it emits it, and that frame is carried through every node downstream before the source’s thread returns to acquiring: each node’s step() runs once per frame, in connection order, on the thread of the source the frame came from. A node with several inputs, a Router merging two sources, runs when the cycle that completes its inputs ends, so it sees one frame from each. Two sources therefore drive two independent chains that meet only where a node takes both.

Around each source the pipeline builds what its residency needs: a Sync that places the source’s samples on the master timeline, and across machines a Link. Those run in the same cycle as the node they wrap, which is why a frame’s position on the timeline is known by the time your node sees it. Time and Synchronisation is the long form.

What a node sees

setup() runs once, before the first frame, with the port context: the sampling rate, the channel count, the frame size, the labels, roles and units. step(data) runs once per frame with data["in"] shaped (frame, channel) and returns what goes out; a node that returns nothing emits nothing that cycle. stop() runs once at the end. Node Design Pattern and Season 7 build one.

The GUI runs on the main thread

app.run() runs the Qt event loop on the main thread, and nothing in the pipeline runs there. A scope’s step() copies each frame into a buffer under a lock, on the pipeline’s thread, and the scope redraws on its own timer, at its own refresh rate, on the GUI thread. A scope is therefore never a brake on the pipeline, and a slow redraw costs frames on the screen, not in the data. Season 7, Episode 5 writes a widget that way.

When the pipeline cannot keep up

A realtime pipeline has a deadline: every frame must be processed before the next one arrives. p.get_load() reports how much of the time each node spends working and names the busiest; a node over 70 % duty is listed as short of headroom, deliberately before frames are late. If frames do arrive faster than they are processed, the backlog is reported and the run is stopped rather than left to grow without bound. Real-Time Performance has the budget and Season 6, Episode 3 reads a report line by line.

Without threads

The server half of a distributed pipeline also runs where no thread can be started: in a browser’s Web Worker, under Pyodide. There the pipeline is driven entirely by arriving frames, each processed in the call that delivered it. A node written as above needs no change for that; a node that starts a thread of its own does. Deployment Scenarios says where that applies.