wasm_python (wasm v0.16.0)
View SourceRun Python from an image of an already-started interpreter.
The same profile and the same tenant contract as wasm_python_command -- main(context),
JSON in and out -- over a reactor rather than a command, so CPython starts
once when the worker starts and every request restores that point.
This is the adapter the Python numbers exist for. Starting CPython inside every
request costs tens of seconds; restoring an image of a started one costs under
a second, and test/audit/PERF.md has both.
{ok, W} = wasm_script_worker:start_link(
wasm_python,
#{path => "test/fixtures/lang/py_reactor.wasm",
lib => "test/fixtures/lang/py_reactor_lib",
root => scratch, limits => wasm_python:limits()}),
{ok, #{result := #{~"answer" := 42}}} =
wasm_script_worker:run(W, #{source => ~"def main(c):\\n return {'answer': c['value'] + 1}\\n",
context => #{~"value" => 41}}).Build the artifact with scripts/build-python-reactor.sh and check what it is
against test/fixtures/lang/PYTHON.md.
An entry set at capture
When the code does not change between requests, give it to the worker as
entry instead of sending it with each one. The capture runs it once, it hands
worker.set_entry the callable to run, and a request that carries no source
then calls that callable with its context: nothing is compiled or imported per
request.
{ok, W} = wasm_script_worker:start_link(
wasm_python,
#{path => "test/fixtures/lang/py_reactor.wasm",
lib => "test/fixtures/lang/py_reactor_lib",
entry => ~"import worker\nworker.set_entry(lambda c: {'answer': c['value'] + 1})\n",
root => scratch, limits => wasm_python:limits()}),
{ok, #{result := #{~"answer" := 42}}} =
wasm_script_worker:run(W, #{context => #{~"value" => 41}}).The capture runs the entry as /main.py through the same path a request's
source takes, so a main it defines is called once, with None. A worker whose
entry does not call worker.set_entry does not start. A request that does carry
a source still runs it, on the same worker.
The context reaches the guest through the worker.context import in both
modes rather than only as a staged file, so it can be as large as
max_request_bytes allows.
Two mounts, and only one of them is the kernel's
The tenant's source and context are staged into the ro mount the kernel
creates and removes. The standard library is not staged: it is 11 MB of files
the adapter ships, and it is preopened directly as /lib from wherever the
build put it. That is the adapter's own dirs entry rather than a mount,
because a mount is a directory the kernel owns and this one it does not.
Writing an adapter is writing host code, which is what makes that allowed.
What an image of CPython freezes
The hash seed, drawn during interpreter startup, is in the image and is
therefore shared by every request that restores it. Re-seeding afterwards is
not available: dictionaries exist by then and string hashes are cached against
the old secret. Rotation means recapturing, which is one init().
docs/snapshots.md says the same thing about every guest.
Summary
Functions
The heap floor a request runner starts with, unless the caller sets one.
What this guest needs, and every one of them is measured rather than rounded.
Capture the interpreter once, between init() and any request.
Functions
-spec defaults(map()) -> wasm_worker_adapter:defaults().
The heap floor a request runner starts with, unless the caller sets one.
It depends on the tier, which Limits names: the worker's resolved limits.
- Compiled,
compile => truewithfuel => infinity, the conditionwasm_jit:entry/3runs generated code under: 1,500,000 words. Unfloored, a request took 23.6 to 25.3 ms at p50 with 18 collections and peaked at 1.40M words. At 1,000,000 it took 20.6 to 21.9 ms with 5, but the heap grew mid-request to 2.88M words; at 1,500,000 it took 20.5 to 21.0 ms with 4 and never grew, and was 0.6 to 1.2 ms (3 to 6%) faster than 1,000,000 at p50 in each of four paired rounds. In a pool of ten it gave 29 to 34% more throughput where 1,000,000 gave 17 to 24% (test/audit/PERF.md, "Compiled CPython's floor, by tier" and "Default floors in a pool"). - Interpreted, anything else: 1,000,000 words, the measured knee. A request fell from 141 to 145 ms at p50 to 44 to 46 ms, and 2,000,000 bought about a millisecond for 60% more peak heap (the first section).
Both fit with the worker's 2x headroom: 3,000,000 words under limits/0's
16,777,216 and under the untrusted preset's 8,388,608.
No capture floor, although capture_min_heap_words => 2_000_000 takes a
worker start from about 92 s to 17 s ("The same floor on the capture, which is
worth more"). Under limits/0's own 16,777,216-word ceiling that floor
kills the capture: three of four fresh starts died on 0.7.0, each after about
18 s, as test/audit/ATTEMPTS.md recorded before. It is only safe beside a
larger max_heap_words, and a default cannot know the caller raised one.
docs/python.md sets the two together, at 33,554,432 words.
-spec limits() -> map().
What this guest needs, and every one of them is measured rather than rounded.
An interpreter does not start under the untrusted preset: PYTHON.md records
that its fuel ceiling does not reach CPython's first line, and that the default
heap bound kills the runner outright. A host raises these knowingly, which is
exactly why an adapter never raises one for you.
Capture the interpreter once, between init() and any request.
The initialisation bindings are trusted and deliberately barren: an empty
work directory, the standard library, and stdio going nowhere. Whatever
init() touches is in the image every request restores, so the less it touches
the less is shared.