Skip to content

API turboframe.process

rafageist edited this page Dec 4, 2026 · 5 revisions

turboframe.process

Source: turboframe/process.py. Generated from the code of TurboFrame 0.3.0 by tools/wiki_api.py.

The processes an application starts die with it, and never ask on its screen.

Language servers, debuggers, scripts, git: none of them may outlive the application, whichever way it ends (its quit command, an error, Ctrl+C, a test that leaves, a kill). Two guards:

  • every process is started with options(): on Linux the kernel sends it SIGTERM when the application dies (PR_SET_PDEATHSIG), even if the application is killed;
  • every process is track()-ed, and kill_all() (when the app closes, and at exit) kills the ones still running.

start() does both for asyncio.create_subprocess_exec. Once the app closes (closing()), start() starts nothing more, and settle() waits until no process is still being started: the app's loop must not close while asyncio starts one (CPython gh-103847, Python 3.11 and 3.12). An app does, when it mounts (another app may have closed on the same loop before, as in tests):

process.opening()

and when it unmounts:

process.closing()
await process.settle()
process.kill_all()

options(group=True) makes the process lead a session (and process group) of its own on POSIX: it has no terminal to ask on (ssh, git), and kill_group() also ends what it started (a wrapper that starts node).

quiet_env() is an environment in which nothing asks or pages: no terminal prompt for credentials (git, its credential manager), no pager.

run() runs a command to its end with its output line by line, the lines it rewrites in place (Receiving objects: 45%) apart, an optional timeout, and cancellation that kills it. communicate() runs one to its end with an input and its whole output, killed (and reaped) when its time runs out. command_line() and split_arguments() show and read a command line as this system's shell does.

Contents

Output, Result, command_line, communicate, split_arguments, closing, kill, kill_all, kill_group, opening, options, quiet_env, run, running, settle, start, track

class Output

class Output
Output(code: int | None, out: bytes, err: bytes, timed_out: bool = False)

What communicate got: the exit code, standard output and error as bytes, and whether the time ran out (the process was killed then).

  • text(which: str = 'out') -> str: out or err decoded as UTF-8, bad bytes replaced.

class Result

class Result
Result(code: int, output: str)

Result(code: 'int', output: 'str')

command_line

def command_line(argv: list[str]) -> str

argv as this system's shell would take it: what a user is shown before a launch.

communicate

async def communicate(*argv: str, input: bytes | None = None, timeout: float | None = None, group: bool = False, **kwargs) -> Output

Run argv with input on its standard input, and wait for it (at most timeout seconds): its exit code and its whole output. Out of time, it is killed (its group, when group), and the output is what it had written until then (timed_out is True). Cancelled (a progress box stopped with Esc), it is killed and the cancel goes on.

It never waits on a prompt: without input its standard input is empty. OSError (a program not found) is the caller's to word. kwargs go to the process (cwd, env).

split_arguments

def split_arguments(text: str, *, strict: bool = False, windows: bool | None = None) -> list[str]

Arguments typed on one line (Run > Parameters) as a program receives them: command_line's inverse.

They are a program's arguments, not a shell's expression: nothing is expanded or interpreted (no variables, no *, no |, >, ; or &&, no %VAR% or ^); those characters are plain text in the arguments. Quotes keep spaces and are not part of the argument; "" (or '') is an empty argument.

  • On POSIX (windows False), the shell's quoting: '...' keeps everything, "..." and outside quotes a backslash escapes the next character.
  • On Windows (windows True; by default, this system's), the Microsoft C runtime's, which a Python program started there applies to its command line and subprocess.list2cmdline writes for: backslashes are plain (C:\Users\me) except before a ", where \" is a quote in the argument and \\" a backslash before a quote that opens or closes; "" inside quotes is a quote.

Bad quoting (a quote left open): with strict, ValueError, for a dialog to say; otherwise the text is split on spaces, the quotes kept as typed.

closing

def closing() -> None

The app on this loop is closing: start() starts nothing more on it.

kill

def kill(process) -> None

Kill process if it still runs (one that just ended is not an error).

kill_all

def kill_all() -> None

Kill every process the application started that still runs.

kill_group

def kill_group(process) -> None

Kill what is left of the process group process leads (started with options(group=True)), the leader too if it still runs. Call it as the leader ends: its id is not reused while the group has members. On Windows, and for a process that leads no group, it kills process alone.

opening

def opening() -> None

An app runs on this loop (again): start() starts processes.

options

def options(*, group: bool = False) -> dict

Keyword arguments for asyncio.create_subprocess_exec. group: on POSIX the process leads a session (and process group) of its own, so it has no terminal to ask on and kill_group also ends what it started.

quiet_env

def quiet_env(base: Mapping[str, str] | None = None) -> dict[str, str]

base (os.environ by default) with prompts and pagers off: what a command started for the application, not for the user at a terminal, runs with (run(..., env=quiet_env())).

run

async def run(argv: Sequence[str], *, cwd: str | PathLike | None = None, env: Mapping[str, str] | None = None, on_line: Callable[[str], None] | None = None, on_progress: Callable[[str], None] | None = None, stdin: str | None = None, timeout: float | None = None, group: bool = False) -> Result

Run argv to its end. Its output (both streams) goes line by line to on_line; the lines it rewrites in place (ending in a lone \r) go to on_progress. stdin: text written to it, else it reads nothing. group: see options; on a timeout or a cancel the whole group is killed.

Raises OSError when it cannot start, TimeoutError after timeout seconds and asyncio.CancelledError when cancelled; the process has ended in both cases. TimeoutError is an OSError too: a caller that maps a failed start catches TimeoutError first:

try:
    result = await process.run(argv, timeout=60)
except TimeoutError:
    raise                       # still a timeout
except OSError as exc:
    raise MyError(f"could not start: {exc}") from None

running

def running() -> list

settle

async def settle(timeout: float = 5.0) -> bool

Wait (at most timeout seconds) until no process is still being started; True when none is.

On Python 3.11 and 3.12 (CPython gh-103847, fixed in 3.13), a task cancelled while asyncio starts its process ends only once asyncio has connected that process's pipes. Closing the loop cancels every task at once, asyncio's own pipe connection among them: the task then waits for pipes that never connect, and the loop never closes. So whatever the app is starting must have started, or failed to, before its loop closes: when the app unmounts, it calls closing() (nothing new starts) and then settles.

start

async def start(*argv, group: bool = False, **kwargs)

asyncio.create_subprocess_exec(*argv, **kwargs) with options(group=group) (which kwargs may extend), track()-ed. Raises asyncio.CancelledError once the app on this loop is closing.

track

def track(process)

Remember process to kill it when the application ends; returns it.

Clone this wiki locally