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: Task

A 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:
__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 Task and overriding on_run.

  • on_prepare – The function to call when the task is prepared. Specifying this is an alternative to subclassing Task and overriding on_prepare.

  • command (list[str])

  • cwd (PathLike[Any] | str | None)

  • env (dict[str, str] | None)

  • 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.CalledProcessError if 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 Process task, the process is not yet running. Use write if 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 Process tasks.

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 the Process task 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: TaskEvent

Base class for events emitted by Process tasks.

class yosys_mau.task_loop.process.OutputEvent(output)

Bases: ProcessEvent

Base class for events containing output of a Process.

Parameters:

output (str)

output: str

The output of the process.

By default this is a single line of output, including trailing newlines.

class yosys_mau.task_loop.process.StdoutEvent(output)

Bases: OutputEvent

Emitted when a Process writes to stdout.

Parameters:

output (str)

class yosys_mau.task_loop.process.StderrEvent(output)

Bases: OutputEvent

Emitted when a Process writes to stderr.

Parameters:

output (str)

class yosys_mau.task_loop.process.ExitEvent(returncode)

Bases: ProcessEvent

Emitted when a Process exits.

Parameters:

returncode (int)

returncode: int

The return code of the process.