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#
| Method | Description |
|---|---|
__construct | Starts the process. |
getStream | Returns the stream of a standard descriptor. |
stop | Ends the process: SIGTERM, then SIGKILL after one second, and waits up to five seconds in all. |
isRunning | Returns true if the process is running. |
isStopped | Returns true if the process is paused by a stop signal. |
getExitCode | Returns the exit code of the process, or false while it runs. |
sendSignal | Sends a signal to the process, and returns false without sending it if the process is not running. |
sigterm | Sends SIGTERM, which a process can catch to shut down. |
sigkill | Sends SIGKILL, which a process cannot catch or ignore. |
sigint | Sends SIGINT, the signal of Ctrl+C. |
sigstop | Sends SIGSTOP, which pauses the process until sigcont(). |
sigcont | Sends SIGCONT, which resumes a process paused by sigstop(). |
sighup | Sends SIGHUP, which many daemons take as a request to reload their configuration. |
read | Waits until the stream has something to read, and returns what is there; an empty string at the end of the stream. |
write | Waits until the stream can take data, and writes as much as it takes; false if the process is not running. |
Examples#
phasync::run(function () {
$process = phasync\Util\Process::run('sleep', ['10']);
phasync::sleep(0.1);
$process->sigterm();
});See also#
Process::runStarts$commandin the background and returns the object that controls it.ProcessInterfaceA child process started withProcess::run(), with its standard streams usable from coroutines.