phasync
The event loop / Examples

The event loop

Examples

Every example of this topic, with its output.

phasync::service#

Example 1
phasync::run(function () {
    phasync::service(function () {
        phasync::sleep(10);   // cancelled when the run has nothing else to do
    });
    phasync::sleep(0.1);
    echo "main ends\n";
});
// run() returns after 0.1 seconds

phasync::isRunning#

Example 1
var_dump(phasync::isRunning());   // false
phasync::run(fn () => var_dump(phasync::isRunning()));   // true

phasync::getFiber#

Example 1
phasync::run(function () {
    $fiber = phasync::getFiber();
    var_dump($fiber->isRunning());   // true
});

DeadlockException#

Example 1
try {
    phasync::run(function () {
        $done = new phasync\Util\WaitGroup();
        $done->add();
        $done->await();     // nobody calls done()
    });
} catch (phasync\DeadlockException $e) {
    echo $e->getMessage(), "\n";
}