Process
process
¶
Defines subprocess execution helpers for the launcher core
Overview
-
run→ Executes an external command to completion -
stream→ Yields the combined stdout/stderr of an external command's execution line by line so a front-end can render live output -
StreamHandle→ An optional cancellation token a caller passes tostreamto stop a long-running command (e.g.docker compose logs -f) on demand from another thread -
Neither prints anything, presentation is the caller's job
StreamHandle
¶
Cancellation token that stops a running stream from another thread
The Problem It Solves
-
streamfollows a command's output by blocking onreadlineuntil the next line arrives -
For a tailing command like
docker compose logs -f, lines can be minutes apart, so the consumer thread sits parked inside that read -
The only way to release the read is to make the command's output stream reach EOF, which means killing the process that holds it
-
That process is created inside
streamand is not visible to whatever owns the stop control (a GUI button, a signal handler), which typically lives on a different thread -
StreamHandleis the shared object that bridges the two. The caller creates it, hands it tostream, and later callscancelto stop the command from the outside
How It Works With stream
-
The caller constructs a
StreamHandleand passes it tostream -
As soon as
streamstarts the subprocess it callsbind, giving the handle the live process to act on -
When the caller invokes
cancel, the whole process tree is killed (see_kill_process_tree). Its stdout closes, the blockedreadlinereturns"", and the generator finishes normally -
A killed process exits non-zero, so
streamcheckscancelledand skips raisingCalledProcessErrorfor that case. A deliberate stop is a normal end, not a command failure
Threading And Ordering
-
cancelis meant to be called from a different thread than the one running the generator -
Killing the tree only signals OS processes, so this is safe
-
bindalso covers the race condition wherecancelis called before the process has started. The handle remembers the request and kills the tree the moment it is bound
Example
cancelled
property
¶
Whether a stop was requested through cancel
stream reads this after the process exits to decide whether a
non-zero exit code is a real failure or the result of a deliberate
stop
Returns:
| Type | Description |
|---|---|
bool
|
|
bool
|
started yet at that moment |
bind(process)
¶
Hands the handle the live process started by stream
Called by stream right after it spawns the subprocess, so a later
cancel has something to terminate. If cancel already ran before
the process existed, the process is terminated immediately here so an
early stop request is not lost
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
process
|
Popen[str]
|
The subprocess |
required |
cancel()
¶
Requests a stop, terminating the streamed process if it is running
Marks the handle cancelled (so stream treats the resulting non-zero
exit as a clean stop) and terminates the bound process. When the
process has not started yet, bind performs the termination once it
does. Safe to call from a thread other than the one using the
generator
run(cmd, *, cwd=None, check=True)
¶
Runs an external command in a subprocess to completion and captures its output
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
list[str]
|
The external command to run and its arguments |
required |
cwd
|
Path | None
|
The directory from which to run the command.
When set to |
None
|
check
|
bool
|
Raises an exception on a non-zero exit status when
|
True
|
Returns:
| Type | Description |
|---|---|
CompletedProcess[str]
|
The completed process with the captured |
Raises:
| Type | Description |
|---|---|
CalledProcessError
|
If the command fails and |
FileNotFoundError
|
If the executable is not found |
stream(cmd, *, cwd=None, check=True, handle=None)
¶
Runs an external command in a subprocess and yields its combined output (stdout + stderr) line by line
handle
-
When a
StreamHandleis given,handle.cancel(typically from another thread) terminates the command early and the resulting non-zero exit is treated as a clean stop rather than a failure -
See
StreamHandlefor more information
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cmd
|
list[str]
|
The external command to run and its arguments |
required |
cwd
|
Path | None
|
The directory from which to run the command.
When set to |
None
|
check
|
bool
|
Raises an exception on a non-zero exit status when
|
True
|
handle
|
StreamHandle | None
|
An optional cancellation token |
None
|
Yields:
| Type | Description |
|---|---|
str
|
Each stripped output line as it is produced |
Returns:
| Type | Description |
|---|---|
int
|
The command's exit code |
Raises:
| Type | Description |
|---|---|
CalledProcessError
|
If the command fails and |
FileNotFoundError
|
If the executable is not found |