phasync
phasync / sleep

Method

phasync::sleep

Pauses the current coroutine for $seconds, and lets the others run meanwhile.

Description#

public static phasync::sleep(
    float $seconds = 0.0
): void

With the default of 0 the coroutine goes to the back of the line: every coroutine that is ready runs before it resumes. A timer wakes the coroutine after $seconds, never earlier. Outside a coroutine it blocks the process for that long, and returns at once for 0.

Inside a coroutine, PHP's sleep() blocks the whole process, unless ext-phasync is loaded. phasync\sleep() is this function under the name of the native one.

Parameters#

$seconds float = 0.0
How long to pause; 0 lets the others run once

Examples#

Example 1
phasync::run(function () {
    phasync::go(function () { phasync::sleep(0.2); echo "slow\n"; });
    phasync::go(function () { phasync::sleep(0.1); echo "fast\n"; });
});   // fast, slow

See also#

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