phpgt / async
Promise-based non-blocking operations.
Fund package maintenance!
phpgt
Installs: 1 142
Dependents: 2
Suggesters: 0
Security: 0
Stars: 0
Watchers: 3
Forks: 2
Open Issues: 5
Requires
- php: >=7.4
- ext-dom: *
- phpgt/promise: ^2.0
Requires (Dev)
- phpstan/phpstan: ^v1.8
- phpunit/phpunit: ^v9.5
This package is auto-updated.
Last update: 2024-10-30 02:10:42 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();