Skip to main content

Mojo struct

Process

struct Process

Create and manage child processes from file executables.

Example usage:

child_process = Process.run("ls", List[String]("-lha"))
if child_process.interrupt():
    print("Successfully interrupted.")

Fields

  • child_pid (c_pid_t): Child process id.
  • status (Optional[ProcessStatus]): Cached status of the process. None if the process has not been waited on yet.

Implemented traits

AnyType, ImplicitlyDestructible

comptime members

__del__is_trivial

comptime __del__is_trivial = False

Methods

__init__

__init__(out self, child_pid: Int)

Struct to manage metadata about child process. Use the run static method to create new process.

Args:

  • child_pid (Int): The pid of child process returned by posix_spawnp that the struct will manage.

__del__

__del__(deinit self)

Waits for the process to exit when the Process object is destroyed.

hangup

hangup(mut self) -> Bool

Send the Hang up signal to the managed child process.

Returns:

Bool: Upon successful completion, True is returned else False.

interrupt

interrupt(mut self) -> Bool

Send the Interrupt signal to the managed child process.

Returns:

Bool: Upon successful completion, True is returned else False.

kill

kill(mut self) -> Bool

Send the Kill signal to the managed child process.

Returns:

Bool: Upon successful completion, True is returned else False.

poll

poll(mut self) -> ProcessStatus

Check if the child process has terminated in a non-blocking way.

This method updates the internal state of the Process object. If the process has terminated, the status is cached.

Returns:

ProcessStatus: A ProcessStatus indicating the status of the process.

Raises:

Error: If waitpid fails.

wait

wait(mut self) -> ProcessStatus

Wait for the child process to terminate (blocking).

This method updates the internal state of the Process object. If the process has terminated, the status is cached.

Returns:

ProcessStatus: A ProcessStatus indicating the process has exited and its status.

Raises:

Error: If waitpid fails or the process does not exit.

run

static run(var path: String, argv: List[String]) -> Self

Spawn new process from file executable.

Args:

  • path (String): The path to the file.
  • argv (List): A list of string arguments to be passed to executable.

Returns:

Self: An instance of Process struct.

Raises:

Error: If the process fails to spawn.

Was this page helpful?