Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

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: about 6 hours ago
JSON representation

Promise-based non-blocking operations.

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.

***


PHP.Gt/Async build status


PHP.Gt/Async code quality


PHP.Gt/Async code coverage


PHP.Gt/Async latest release


PHP.Gt/Async documentation

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();
```