phasync
Blocking calls and the extension / sleep

Function

sleep

Suspends the current coroutine for $seconds seconds, letting the others run.

Description#

function sleep(
    float $seconds = 0.0
): void

The same as phasync::sleep, under the name of the native function: with use function phasync\sleep; in a file, its sleep() calls wait without blocking the process. The native sleep() takes whole seconds; this takes a float. Outside a coroutine it blocks, as usleep() does.

Parameters#

$seconds float = 0.0
How long to wait; 0 or less only lets the other coroutines run first

Examples#

Example 1
use function phasync\sleep;

phasync::run(function () {
    phasync::go(fn() => print("tick\n"));
    sleep(0.5);                        // the coroutine above runs meanwhile
});

See also#

  • phasync::sleepPauses the current coroutine for $seconds, and lets the others run meanwhile.