|
| 1 | +Promise-based non-blocking operations. |
| 2 | +====================================== |
| 3 | + |
| 4 | +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. |
| 5 | + |
| 6 | +This repository provides the concepts of a `Loop`, different `Timer` implementations and a publish-subscribe model for `Event` objects. |
| 7 | + |
| 8 | +*** |
| 9 | + |
| 10 | +<a href="https://github.com/PhpGt/Async/actions" target="_blank"> |
| 11 | + <img src="https://badge.status.php.gt/async-build" alt="PHP.Gt/Async build status" /> |
| 12 | +</a> |
| 13 | +<a href="https://scrutinizer-ci.com/g/PhpGt/Async" target="_blank"> |
| 14 | + <img src="https://badge.status.php.gt/async-quality" alt="PHP.Gt/Async code quality" /> |
| 15 | +</a> |
| 16 | +<a href="https://scrutinizer-ci.com/g/PhpGt/Async" target="_blank"> |
| 17 | + <img src="https://badge.status.php.gt/async-coverage" alt="PHP.Gt/Async code coverage" /> |
| 18 | +</a> |
| 19 | +<a href="https://packagist.PhpGt/packages/PhpGt/Async" target="_blank"> |
| 20 | + <img src="https://badge.status.php.gt/async-version" alt="PHP.Gt/Async latest release" /> |
| 21 | +</a> |
| 22 | +<a href="http://www.php.gt/Async" target="_blank"> |
| 23 | + <img src="https://badge.status.php.gt/async-docs" alt="PHP.Gt/Async documentation" /> |
| 24 | +</a> |
| 25 | + |
| 26 | +Example usage |
| 27 | +------------- |
| 28 | + |
| 29 | +A loop with three individual timers at 1 second, 5 seconds and 10 seconds. |
| 30 | + |
| 31 | +```php |
| 32 | +$timeAtScriptStart = microtime(true); |
| 33 | + |
| 34 | +$timer = new IndividualTimer(); |
| 35 | +$timer->addTriggerTime($timeAtScriptStart + 1); |
| 36 | +$timer->addTriggerTime($timeAtScriptStart + 5); |
| 37 | +$timer->addTriggerTime($timeAtScriptStart + 10); |
| 38 | + |
| 39 | +$timer->addCallback(function() use($timeAtScriptStart) { |
| 40 | + $now = microtime(true); |
| 41 | + $secondsPassed = round($now - $timeAtScriptStart); |
| 42 | + echo "Number of seconds passed: $secondsPassed", PHP_EOL; |
| 43 | +}); |
| 44 | + |
| 45 | +$loop = new Loop(); |
| 46 | +$loop->addTimer($timer); |
| 47 | +echo "Starting...", PHP_EOL; |
| 48 | +$loop->run(); |
| 49 | +``` |
0 commit comments