Skip to main content

Module worker

Module worker 

Source
Expand description

Keeping a process alive between calls.

Starting an interpreter costs more than most calls do — around twenty milliseconds for Python, against microseconds of actual work — so a program calling repeatedly pays that over and over for nothing. A worker pays it once and then answers as fast as a pipe allows.

§What changes, and what does not

The messages are the same ones §7.1 of the protocol specifies. What changes is only the framing: with the stream staying open, end of input can no longer mean end of message, so each message is a line. A child that reads lines works either way — one-shot, the parent closes the stream and the loop ends after a single message — which is why the generated harnesses do that and need no mode of their own.

§What it costs

State survives between calls. That is the whole saving and the whole risk: a module holding something over from the last call is now able to get it wrong, where one process per call could not. Hence off by default, and hence this: a worker that fails, times out, or dies is dropped rather than reused. Once an exchange goes wrong the stream’s position is unknown, and a stale answer read as the next call’s result would be far worse than starting again.

Structs§

Workers
The workers this process is keeping alive, one lane per environment.