Amp is a non-blocking concurrency framework for PHP. It provides an event loop, promises and streams as a base for asynchronous programming.
Promises in combination with generators are used to build coroutines, which allow writing asynchronous code just like synchronous code, without any callbacks.
This package can be installed as a Composer dependency.
composer require amphp/amp
Documentation can be found on amphp.org as well as in the ./docs
directory.
- PHP 7.0+
Extensions are only needed if your app necessitates a high numbers of concurrent socket connections.
This simple example uses our Artax HTTP client to fetch multiple HTTP resources concurrently.
<?php
use Amp\Artax\Response;
use Amp\Loop;
require __DIR__ . '/../vendor/autoload.php';
Loop::run(function () {
$uris = [
"https://google.com/",
"https://github.com/",
"https://stackoverflow.com/",
];
$client = new Amp\Artax\DefaultClient;
$client->setOption(Amp\Artax\Client::OP_DISCARD_BODY, true);
try {
foreach ($uris as $uri) {
$promises[$uri] = $client->request($uri);
}
$responses = yield $promises;
foreach ($responses as $uri => $response) {
print $uri . " - " . $response->getStatus() . $response->getReason() . PHP_EOL;
}
} catch (Amp\Artax\HttpException $error) {
// If something goes wrong Amp will throw the exception where the promise was yielded.
// The Client::request() method itself will never throw directly, but returns a promise.
print $error->getMessage() . PHP_EOL;
}
});
Further examples can be found in the ./examples
directory of this repository as well as in the ./examples
directory of our other libraries
amphp/amp
follows the semver semantic versioning specification like all other amphp
packages.
Version | Bug Fixes Until | Security Fixes Until |
---|---|---|
2.x | TBA | TBA |
1.x | 2017-12-31 | 2018-12-31 |
Compatible packages should use the amphp
topic on GitHub.
If you discover any security related issues, please email [email protected]
or [email protected]
instead of using the issue tracker.
The MIT License (MIT). Please see LICENSE
for more information.