phasync
Blocking calls and the extension / Examples

Blocking calls and the extension

Examples

Every example of this topic, with its output.

MySQLiAwaiter#

Example 1
phasync::run(function () use ($mysqli) {
    $mysqli->query('SELECT SLEEP(1)', MYSQLI_ASYNC);
    MySQLiAwaiter::await($mysqli);
    $result = $mysqli->reap_async_query();
});

file_get_contents#

Example 1
use function phasync\file_get_contents;

$config = file_get_contents(__DIR__ . '/config.json');

file_put_contents#

Example 1
use function phasync\file_put_contents;

file_put_contents('/tmp/log.txt', "started\n", FILE_APPEND);

flock#

Example 1
use function phasync\flock;

$fp = fopen('/tmp/job.lock', 'c');
flock($fp, LOCK_EX);    // other coroutines run while another process holds the lock

sleep#

Example 1
use function phasync\sleep;

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