Running Subprocesses as Tasks¶
In the mau task loop, subprocesses run as tasks.
The output of a subprocess is made available using events.
It’s possible to launch subprocesses by creating instances of either Process or of a user defined subclass of Process.
- class yosys_mau.task_loop.process.Process(command, *, cwd=None, env=None, interact=False)¶
Bases:
TaskA task that runs and supervises a subprocess.
The constructor creates a new task as child task of the current task and schedule it to run in the current ask loop.
- Parameters:
on_run – The function to call when the task is run. Specifying this is an alternative to subclassing
Taskand overridingon_run.on_prepare – The function to call when the task is prepared. Specifying this is an alternative to subclassing
Taskand overridingon_prepare.cwd (InlineContextVar[PathLike[Any] | str])
env (InlineContextVar[TaskContextDict[str, str]])
interact (bool)
- __init__(command, *, cwd=None, env=None, interact=False)¶
The constructor creates a new task as child task of the current task and schedule it to run in the current ask loop.
- Parameters:
on_run – The function to call when the task is run. Specifying this is an alternative to subclassing
Taskand overridingon_run.on_prepare – The function to call when the task is prepared. Specifying this is an alternative to subclassing
Taskand overridingon_prepare.interact (bool)
- property shell_command: str¶
The command as a string that can be executed in a shell.
This includes changing the working directory (in a sub-shell) if necessary. This is intended for log output that can be copy-pasted into a shell to manually re-run the command.
- on_exit(returncode)¶
Called when the process exits.
By default, this raises a
subprocess.CalledProcessErrorif the process exited with a non-zero return code. Override this to change this behavior.- Parameters:
returncode (int)
- Return type:
None
- property stdin: StreamWriter¶
The stdin stream of the process.
Note that this is only available for interactive processes and only while the process is running. In particular, after creating the
Processtask, the process is not yet running. Usewriteif you need to send data to the process before it starts.
- write(data)¶
Write data to the stdin stream of the process.
This is only available for interactive processes.
Data written before the process starts is buffered and sent to the process as soon as it does.
- Parameters:
data (str)
- Return type:
None
- close_stdin()¶
Close the stdin stream of the process.
When this is called before the process starts, it is buffered and the stream is closed directly after writing the already buffered data after the process starts.
- Return type:
None
- log_output()¶
Log the output of the process.
- Return type:
None
Context Variables¶
- class yosys_mau.task_loop.process.ProcessContext¶
Task context variables for
Processtasks.- cwd = os.getcwd()¶
The working directory for newly spawned processes.
Note that
Process’s constructor takes a snapshot of this value, so parent updates between the creation of theProcesstask and the actual start of the process are ignored.Changing the default value outside of a task loop changes the working directory using
os.chdir.
- env: TaskContextDict[str, str]¶
The environment for newly spawned processes.
Events¶
- class yosys_mau.task_loop.process.ProcessEvent¶
Bases:
TaskEventBase class for events emitted by
Processtasks.
- class yosys_mau.task_loop.process.OutputEvent(output)¶
Bases:
ProcessEventBase class for events containing output of a
Process.- Parameters:
output (str)
- class yosys_mau.task_loop.process.StdoutEvent(output)¶
Bases:
OutputEventEmitted when a
Processwrites to stdout.- Parameters:
output (str)
- class yosys_mau.task_loop.process.StderrEvent(output)¶
Bases:
OutputEventEmitted when a
Processwrites to stderr.- Parameters:
output (str)