https://github.com/phpgt/async
Promise-based non-blocking operations.
https://github.com/phpgt/async
async async-await async-programming asynchronous asynchronous-programming php-async phpgt
Last synced: 6 months ago
JSON representation
Promise-based non-blocking operations.
- Host: GitHub
- URL: https://github.com/phpgt/async
- Owner: phpgt
- Created: 2020-11-11T11:13:17.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2025-04-22T13:53:45.000Z (9 months ago)
- Last Synced: 2025-07-18T21:15:55.429Z (6 months ago)
- Topics: async, async-await, async-programming, asynchronous, asynchronous-programming, php-async, phpgt
- Language: PHP
- Homepage:
- Size: 115 KB
- Stars: 0
- Watchers: 2
- Forks: 2
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- Security: SECURITY.md
Awesome Lists containing this project
README
Promise-based non-blocking operations.
======================================
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.
```php
$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();
```