Skip to content

Processes

rafageist edited this page Dec 3, 2026 · 5 revisions

Processes

turboframe.process makes sure the processes your application starts die with it and never ask anything on its screen. Language servers, debuggers, scripts and git must not outlive the application, however it ends: its exit command, an error, Ctrl+C, a test that leaves, a kill.

Use it for every program your application starts: a build, a linter, git, a language server, a debugger
Import from turboframe import process
Needs nothing beyond TurboFrame; it imports nothing of Textual
Extend quiet_env() for your environment; group=True for a program that starts others

Running a command

from turboframe import process

result = await process.run(["python", "-m", "pytest", "-q"], cwd=project, on_line=output.append,
                           on_progress=status.set, timeout=600, env=process.quiet_env())
if not result.ok:
    print(result.code, result.output)

run(argv, cwd=None, env=None, on_line=None, on_progress=None, stdin=None, timeout=None, group=False) runs a command to its end:

  • on_line gets its output line by line, both streams;
  • on_progress gets the lines it rewrites in place (Receiving objects: 45%, ending in a lone \r);
  • stdin is text to write to it; without it, the command reads nothing;
  • Result(code, output) has .ok when the code is 0;
  • it raises OSError when the command cannot start, and TimeoutError after timeout seconds. TimeoutError is an OSError too, so catch it first. Cancelling the task kills the process. In both cases the process has ended when the exception reaches you.
  • group=True: on Linux the command leads a session of its own. It then has no terminal to ask on (ssh, git), and a timeout or a cancel kills everything it started.

quiet_env() is the environment with prompts and pagers off (GIT_TERMINAL_PROMPT=0, GCM_INTERACTIVE=never, PAGER=cat): give it to anything that might ask for a password.

Long-running processes

from asyncio.subprocess import PIPE

server = await process.start("pyright-langserver", "--stdio", stdin=PIPE, stdout=PIPE)

start(*argv, group=False, **kwargs) is asyncio.create_subprocess_exec with two guards. On Linux the kernel sends the process SIGTERM when your application dies, even when it is killed (PR_SET_PDEATHSIG). And the process is tracked, so kill_all(), which runs when the application closes and at exit, kills it if it still runs. kill(p) and kill_group(p) end one. running() lists those still alive.

Closing

An application on Textual's loop does, when it mounts, process.opening(); and when it unmounts, process.closing(), await process.settle(), process.kill_all(). DesktopApp does both for you. settle exists for a CPython bug (gh-103847, in 3.11 and 3.12): the loop could not close while asyncio was starting a process.

process imports nothing of Textual: a command-line tool can use it.

A command run to its end

communicate(*argv, input=None, timeout=None, group=False, **kwargs) runs a program with an input and waits for all of its output, at most timeout seconds. kwargs go to the process (cwd, env). It returns an Output(code, out, err, timed_out): out and err are bytes, text("out") and text("err") decode them:

from turboframe import process

done = await process.communicate("php", "-l", input=text.encode(), timeout=10)
if done.timed_out:
    ...                                  # killed and reaped
elif done.code != 0:
    show(done.text("err"))

Its standard input is empty unless you give one, so it never waits on a prompt. Out of time it is killed (its whole group with group=True) and reaped, and the Output has timed_out=True and what the program had written until then in out and err (since v0.2.1; v0.2.0 gave them empty), which often says why it hung. Cancelled, from a progress box's Esc, it is killed and the cancel goes on. A program that is not there raises OSError, yours to word.

A command line, shown and read back

command_line(argv) shows a command as this system's shell would take it. split_arguments(text, *, strict=False, windows=None) does the reverse: it reads arguments typed on one line (Run > Parameters) into the list a program receives.

from turboframe.process import command_line, split_arguments

split_arguments('--name "two words" -v')          # ['--name', 'two words', '-v']
split_arguments('"" x')                            # ['', 'x']: an empty argument
split_arguments(r'C:\Users\me\a.txt', windows=True)  # ['C:\\Users\\me\\a.txt']: Windows paths as typed
split_arguments('x "unclosed')                     # ['x', '"unclosed']: bad quoting splits on spaces
split_arguments('x "unclosed', strict=True)        # ValueError, for your dialog to show
  • The quotes keep spaces and are not part of the argument. On Linux and macOS the rules are the shell's (shlex): single quotes keep everything, a backslash escapes. On Windows they are the C runtime's, the ones a program started there applies and command_line writes for: backslashes are plain, except before a quote. windows= picks the rules whatever the system.
  • It reads arguments, not a shell expression. Nothing is expanded or interpreted: $HOME, %PATH%, *.py, |, >, ; and && reach the program as typed. To run a shell expression, run the shell (["sh", "-c", text], ["cmd", "/c", text]) knowingly.
  • With strict=True, a quote left open is a ValueError; without it, the line is split on spaces with the quotes as typed (what v0.2.0 did).
  • Before v0.2.1, on Windows the quotes stayed inside the arguments ('"two words"').

To show what a command prints, in a window that follows it, see Log and Output Windows.

API: turboframe.process.

Clone this wiki locally