phpgt / async
Promise-based non-blocking operations.
Fund package maintenance!
phpgt
Requires
- php: >=8.1
- ext-dom: *
- phpgt/promise: ^2.0
Requires (Dev)
- phpmd/phpmd: ^2.13
- phpstan/phpstan: ^1.10
- phpunit/phpunit: ^10.1
- squizlabs/php_codesniffer: ^3.7
This package is auto-updated.
Last update: 2025-03-07 17:27:22 UTC
README
To be able to run asynchronous code in PHP, a loop needs to run in the background to observe and dispatch events, and handle the resolution of promises.
This repository provides the concepts of a Loop
, different Timer
implementations and a publish-subscribe model for Event
objects.
Example usage
A loop with three individual timers at 1 second, 5 seconds and 10 seconds.
$timeAtScriptStart = microtime(true); $timer = new IndividualTimer(); $timer->addTriggerTime($timeAtScriptStart + 1); $timer->addTriggerTime($timeAtScriptStart + 5); $timer->addTriggerTime($timeAtScriptStart + 10); $timer->addCallback(function() use($timeAtScriptStart) { $now = microtime(true); $secondsPassed = round($now - $timeAtScriptStart); echo "Number of seconds passed: $secondsPassed", PHP_EOL; }); $loop = new Loop(); $loop->addTimer($timer); echo "Starting...", PHP_EOL; $loop->run();