20

Amp - Asynchronous concurrency made simple ⋅ amphp

 4 years ago
source link: https://amphp.org
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

Leading PHP into an asynchronous future

Amp is an event-driven concurrency framework for PHP providing primitives to manage cooperative multitasking building upon an event loop, promises, and asynchronous iterators.

Event driven programming requires a different mindset, so Amp provides synchronous feeling APIs to ease the transition.

Get Started

tree_swing.svg
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.

Basic Building Blocks

Event Loop

The event loop is the main task scheduler of every asynchronous application. It dispatches associ­ated handlers once the registered events happen. Read more about the event loop

Promises

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. Read more about promises

Coroutines

Coroutines are interruptible functions that can be paused and resumed. Amp uses Generators to allow promise consumption without callbacks. Read more about coroutines

Advanced Building Blocks

Iterators

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. Read more about iterators

Streams

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. Read more about streams


Use Cases

Amp can be used wherever you have to wait for multiple I/O activities to happen without them having to happen in a specific order (sequentially). It can be used in all SAPIs reaching from Apache and PHP-FPM to PHP-CLI. If you don't have a long running application, you might find Amp\Promise\wait() helpful.

Multiplexing I/O

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.

Reacting to I/O

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.


Compatible Packages

Amphp offers a number of high-quality packages ranging from basic network components to more advanced components like our Aerys HTTP server. All compatible packages should use the amphp tag on GitHub. Many packages are listed on our dedicated Packages page.

An adapter is also provided to allow Amp to operate ReactPHP components.


Sounds Interesting?


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK