phasync
Processes / ProcessInterface

Interface

ProcessInterface

A child process started with Process::run(), with its standard streams usable from coroutines.

Introduction#

read() and write() wait for their stream without blocking the other coroutines. When the process has ended and isRunning() or another status call has noticed it, its streams are closed, so read the output before asking.

Class synopsis#

interface ProcessInterface {
    /* Methods */
    public ProcessInterface::stop(): bool
    public ProcessInterface::isRunning(): bool
    public ProcessInterface::isStopped(): bool
    public ProcessInterface::getExitCode(): int|false
    public ProcessInterface::sendSignal(int $signal = 15): bool
    public ProcessInterface::read(int $fd = self::STDOUT): string|false
    public ProcessInterface::write(string $data, int $fd = self::STDIN): int|false
    public ProcessInterface::getStream(int $fd): mixed
}

Constants#

NameValueDescription
STDIN0
STDOUT1
STDERR2

Methods#

MethodDescription
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.
readWaits until the stream has something to read, and returns what is there.
writeWaits until the stream can take data, and writes as much as it takes.
getStreamReturns the stream of a standard descriptor, for stream_select(), phasync::readable() and the like.

Examples#

Example 1
phasync::run(function () {
    $cat = phasync\Util\Process::run('cat');
    $cat->write("ping\n");
    echo $cat->read();            // ping
    $cat->stop();
});

See also#

  • Process::runStarts $command in the background and returns the object that controls it.