phasync
phasync / idle

Method

phasync::idle

Suspends the current coroutine until the event loop has had nothing to run for at least $after seconds: the ready queue and the callback queue are both empty, and an immediate poll finds no I/O ready.

Description#

public static phasync::idle(
    float $after = 0.0
): void

Waiters wake one per such idle moment, oldest first: each wakes exactly one coroutine, then the loop measures the next idle moment from scratch for whoever is waiting next. That is what makes it fit an admission gate: let one more in, then re-measure, rather than a fixed number that starves whichever requests are waiting on slow I/O.

Parameters#

$after float = 0.0
Seconds the loop must have had nothing runnable, before this coroutine wakes

Errors / Exceptions#

ExceptionWhen
CancelledExceptionIf the coroutine is cancelled while waiting

Examples#

Example 1
phasync::run(function () {
    phasync::go(function () {           // keeps the loop busy for a while
        for ($i = 0; $i < 1000; ++$i) {
            phasync::yield();
        }
        echo "busy done\n";
    });
    phasync::idle();                    // returns only once the loop above has nothing left
    echo "idle\n";                       // after "busy done"
});

See also#

  • phasync::yieldSuspends the current coroutine until the loop has made its next round, after the coroutines that were ready have run.
  • phasync::sleepPauses the current coroutine for $seconds, and lets the others run meanwhile.