phasync
phasync / shutdown

Method

phasync::shutdown

Stop the program's coroutines: every coroutine but the caller, whatever frame it is in, gets $exception (a ShutdownException, which is a CancelledException) at its wait, as phasync::cancel() does, and has up to $window seconds to clean up: finally blocks, and phasync::finally() callbacks, which may wait. Returns how many are still running after that, for the caller to decide (for example to exit, dropping them).

Description#

public static phasync::shutdown(
    float $window = 1.0,
    ?ShutdownException $exception = null
): int

Parameters#

$window float = 1.0
Seconds to wait at most for them to end
$exception ?ShutdownException = null
What they get; a plain ShutdownException by default

Return values#

The coroutines still running after the window int

Errors / Exceptions#

ExceptionWhen
LogicExceptionOutside a coroutine

Examples#

Example 1
phasync::go(function () {
    phasync::signal(SIGTERM);
    if (phasync::shutdown(2.0) > 0) {
        exit(1);   // some coroutines didn't stop in time
    }
});