Amp is an event-driven concurrency framework for PHP providing primitives to manage cooperative multitasking building upon an event loop, and promises.
Event driven programming requires a different mindset, so Amp provides synchronous feeling APIs to ease the transition.
A wide range of packages allows creating entire applications using non-blocking I/O and taking advantage of long running processes. It's also possible to integrate Amp into existing applications for concurrent data access.
try {
$request = new Request("https://amphp.org/");
$response = yield $http->request($request);
if ($response->getStatus() !== 200) {
throw new HttpException;
}
} catch (HttpException $e) {
// handle error
}
Consumption of asynchronous results was traditionally solved via callbacks. Promises shift the callback from a parameter to the return value, while coroutines automate the boilerplate of callbacks entirely.
Amp implements coroutines using PHP's generators to avoid callback or then()
hells.
Promise consumption works without callbacks and allows ordinary catch
clauses just
as synchronous code for handling errors.
The event loop is the main task scheduler of every asynchronous application. It dispatches associated handlers once the registered events happen.
A promise is a placeholder for the result of an asynchronous operation. While synchronous programs block until a result is available, asynchronous programs return a placeholder which gets filled in with the result at a later time.
Coroutines are interruptible functions that can be paused and resumed. Amp uses
Generator
s
to allow promise consumption without callbacks.
Asynchronous iterators allow the consumption of collections of values, one at a time. Instead of resolving a promise once all results are available, iterators make it possible to consume items of a collection as soon as they become available.
Amp provides a stream abstraction that makes working with non-blocking I/O way easier. Don't worry about read- and write-watchers, buffering, and backpressure.
Amp\Promise\wait()
helpful.
The key benefit of non-blocking I/O is that it can be multiplexed. Multiple I/O requests can happen concurrently. The event loop simply waits for any I/O event to occurr instead of waiting for a single I/O operations. This enables parallel HTTP requests, SQL queries or any other I/O related activity.
Maybe you're not interested in speeding up your application by using concurrent I/O (we bet you are), but want to write a daemon that only wakes up the CPU if some I/O event occurred so you can react to that event? Amp's event loop provides exactly that. It lets you register callbacks to react to I/O events.
Amphp offers a number of high-quality packages ranging from basic network components to more advanced
components like our HTTP application server. All compatible packages should use the amphp
tag on GitHub. Many packages are listed on our dedicated Packages page.
Every ReactPHP library is compatible with Amp, so you don't need to choose between those two.