Blocking calls and the extension
Examples
Every example of this topic, with its output.
MySQLiAwaiter#
phasync::run(function () use ($mysqli) {
$mysqli->query('SELECT SLEEP(1)', MYSQLI_ASYNC);
MySQLiAwaiter::await($mysqli);
$result = $mysqli->reap_async_query();
});file_get_contents#
use function phasync\file_get_contents;
$config = file_get_contents(__DIR__ . '/config.json');file_put_contents#
use function phasync\file_put_contents;
file_put_contents('/tmp/log.txt', "started\n", FILE_APPEND);flock#
use function phasync\flock;
$fp = fopen('/tmp/job.lock', 'c');
flock($fp, LOCK_EX); // other coroutines run while another process holds the locksleep#
use function phasync\sleep;
phasync::run(function () {
phasync::go(fn() => print("tick\n"));
sleep(0.5); // the coroutine above runs meanwhile
});