phasync
phasync / signal

Method

phasync::signal

Suspends the current coroutine until one of $signals (POSIX signal numbers such as SIGTERM) arrives, and returns the one that did. Any number of coroutines may wait for the same signal: each is woken. The waiter runs as an ordinary coroutine afterwards, so it may await, write and close connections; the signal's handler only records it, and the event loop wakes the waiters between coroutines, never inside one. A signal that came before the wait began doesn't end it.

Description#

public static phasync::signal(
    array|int $signals,
    float $timeout = PHP_FLOAT_MAX
): int

phasync takes the signal over with its own handler until the outermost phasync::run() returns, then puts the previous handler back; meanwhile its handler still calls a previous pcntl_signal() handler. phasync::onSignal adds code that runs inside it. It works with and without pcntl_async_signals().

Parameters#

$signals array|int
The signal, or signals, to wait for
$timeout float = PHP_FLOAT_MAX
Seconds to wait at most

Return values#

The signal that arrived int

Errors / Exceptions#

ExceptionWhen
TimeoutExceptionIf none arrives in time
LogicExceptionOutside a coroutine

Examples#

Example 1
phasync::go(function () use ($server) {
    phasync::signal([SIGTERM, SIGINT]);
    $server->close();   // stop accepting, let the requests in flight finish
});