phasync
Processes / Process

Class

Process

Starts a child process whose standard streams are used from coroutines without blocking the others.

Class synopsis#

final class Process {
    /* Methods */
    public static Process::run(
        string $command,
        array $arguments = [],
        ?string $cwd = null,
        ?array $env = null
    ): ProcessInterface
}

Methods#

MethodDescription
runStarts $command in the background and returns the object that controls it.

Examples#

Example 1
phasync::run(function () {
    $process = phasync\Util\Process::run('echo', ['hello']);

    echo $process->read();                 // hello
    while ($process->isRunning()) {
        phasync::sleep(0.01);
    }
    var_dump($process->getExitCode());     // int(0)
});

See also#

  • ProcessInterfaceA child process started with Process::run(), with its standard streams usable from coroutines.
  • ProcessRunnerThe implementation behind Process::run(): starts a child process and controls it.