phasync
Processes / ProcessRunner

Class

ProcessRunner

The implementation behind Process::run(): starts a child process and controls it.

Introduction#

Standard input, output and error are sockets that are set to non-blocking, so a coroutine waiting for the child does not block the others. Use Process::run() instead of constructing it. The signal methods are shortcuts for sendSignal().

Class synopsis#

final class ProcessRunner implements ProcessInterface {
    /* Methods */
    public ProcessRunner::__construct(array $command, ?string $cwd = null, ?array $env = null)
    public ProcessRunner::getStream(int $fd): mixed
    public ProcessRunner::stop(): bool
    public ProcessRunner::isRunning(): bool
    public ProcessRunner::isStopped(): bool
    public ProcessRunner::getExitCode(): int|false
    public ProcessRunner::sendSignal(int $signal = 15): bool
    public ProcessRunner::sigterm(): bool
    public ProcessRunner::sigkill(): bool
    public ProcessRunner::sigint(): bool
    public ProcessRunner::sigstop(): bool
    public ProcessRunner::sigcont(): bool
    public ProcessRunner::sighup(): bool
    public ProcessRunner::read(int $fd = phasync\Util\ProcessInterface::STDOUT): string|false
    public ProcessRunner::write(
        string $data,
        int $fd = phasync\Util\ProcessInterface::STDIN
    ): int|false
}

Methods#

MethodDescription
__constructStarts the process.
getStreamReturns the stream of a standard descriptor.
stopEnds the process: SIGTERM, then SIGKILL after one second, and waits up to five seconds in all.
isRunningReturns true if the process is running.
isStoppedReturns true if the process is paused by a stop signal.
getExitCodeReturns the exit code of the process, or false while it runs.
sendSignalSends a signal to the process, and returns false without sending it if the process is not running.
sigtermSends SIGTERM, which a process can catch to shut down.
sigkillSends SIGKILL, which a process cannot catch or ignore.
sigintSends SIGINT, the signal of Ctrl+C.
sigstopSends SIGSTOP, which pauses the process until sigcont().
sigcontSends SIGCONT, which resumes a process paused by sigstop().
sighupSends SIGHUP, which many daemons take as a request to reload their configuration.
readWaits until the stream has something to read, and returns what is there; an empty string at the end of the stream.
writeWaits until the stream can take data, and writes as much as it takes; false if the process is not running.

Examples#

Example 1
phasync::run(function () {
    $process = phasync\Util\Process::run('sleep', ['10']);
    phasync::sleep(0.1);
    $process->sigterm();
});

See also#

  • Process::runStarts $command in the background and returns the object that controls it.
  • ProcessInterfaceA child process started with Process::run(), with its standard streams usable from coroutines.