Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Task cancellation

Call cancel() when work is no longer useful, but design for the possibility that it has already started. Whether Pyroxide can stop it depends on its state and execution boundary.

Task state and modecancel()Outcome
Pending, any modeTrueWork is skipped; status becomes Cancelled
Running, isolatedTrue after terminationWorker process is terminated; status becomes Cancelled
Running, in-process PythonFalseCallable continues and its real result is preserved
Running, in-process WASM or nativeFalseGuest or native call continues
TerminalFalseStatus and result remain unchanged
handle = task_function(payload)
if handle.cancel():
    print("Cancellation took effect")
else:
    print("The task may already be running or finished")

Calling result() on a cancelled task raises RuntimeError("Task cancelled").

Python threads, foreign native calls, and a running WASM invocation cannot be interrupted safely at an arbitrary instruction. Use cooperative cancellation inside your callable when you need finer control, or use isolated=True when process termination is acceptable.

WASM execution timeouts are separate from user cancellation. They trap a guest after its epoch deadline; see WebAssembly.

A timeout passed to result() or result_async() also stops only the wait; it does not cancel the task. See Getting started for handle lifetime after a timeout.