Repository navigation
API 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, andkill_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.
Output, Result, command_line, communicate, split_arguments, closing, kill, kill_all, kill_group, opening, options, quiet_env, run, running, settle, start, track
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:outorerrdecoded as UTF-8, bad bytes replaced.
class Result
Result(code: int, output: str)Result(code: 'int', output: 'str')
def command_line(argv: list[str]) -> strargv as this system's shell would take it: what a user is shown before a launch.
async def communicate(*argv: str, input: bytes | None = None, timeout: float | None = None, group: bool = False, **kwargs) -> OutputRun 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).
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 (
windowsFalse), the shell's quoting:'...'keeps everything,"..."and outside quotes a backslash escapes the next character. - On Windows (
windowsTrue; by default, this system's), the Microsoft C runtime's, which a Python program started there applies to its command line andsubprocess.list2cmdlinewrites 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.
def closing() -> NoneThe app on this loop is closing: start() starts nothing more on it.
def kill(process) -> NoneKill process if it still runs (one that just ended is not an error).
def kill_all() -> NoneKill every process the application started that still runs.
def kill_group(process) -> NoneKill 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.
def opening() -> NoneAn app runs on this loop (again): start() starts processes.
def options(*, group: bool = False) -> dictKeyword 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.
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())).
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) -> ResultRun 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 Nonedef running() -> listasync def settle(timeout: float = 5.0) -> boolWait (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.
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.
def track(process)Remember process to kill it when the application ends; returns it.
TurboFrame · part of Divengine · GPL-3.0-or-later · report an issue
Tutorial
Examples
The desktop
- The Desktop
- Application Mixins
- Windows and Lists
- Dialogs
- Histories
- Progress
- Themes and Colors
- Glyphs
- Settings and Recent Files
- Command Palette
Editing
Tasks
Languages
Running
Reference
Links