forked from amphp/amphp.github.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
157 lines (139 loc) · 6.94 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
---
title: Amp - Asynchronous concurrency made simple
permalink: /
layout: base
---
{% include shared-asset-path.jekyll %}
<div class="content site-width">
<div class="cols">
<div>
<h1>
Leading PHP into an asynchronous future
</h1>
<p>
Amp is an event-driven concurrency framework for PHP providing primitives to manage cooperative multitasking building upon an
event loop, and promises.
</p>
<p>
Event driven programming requires a different mindset, so Amp provides synchronous feeling APIs to ease the transition.
</p>
<p>
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.
</p>
<p>
<a href="/getting-started" class="highlight-area-cta">Get Started <i class="fa fa-angle-right"></i></a>
</p>
</div>
<div class="text-center no-mobile">
<img src="{{ shared_asset_path }}/img/undraw/tree_swing.svg" width="300"/>
</div>
</div>
</div>
<div class="content site-width">
<div class="cols" style="margin: 50px 0;">
<div style="flex: 60 60 0; align-self: center">
<div>
<pre style="border: 0; padding: 0"><code class="php">try {
$request = new Request("https://amphp.org/");
$response = yield <span class="help-bubble" title="Promise consumption without callbacks"></span> $http->request($request);
if ($response->getStatus() !== 200) {
<span class="help-bubble" title="Throw normal exceptions to indicate errors"></span> throw new HttpException;
}
} catch (HttpException $e) <span class="help-bubble" title="Ordinary catch clauses to handle errors"></span> {
// handle error
}</code></pre>
</div>
</div>
<div style="flex: 40 40 0; align-self: center">
<p>
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.
</p>
<p>
Amp implements coroutines using PHP's generators to avoid callback or <code>then()</code> hells.
Promise consumption works without callbacks and allows ordinary <code>catch</code> clauses just
as synchronous code for handling errors.
</p>
</div>
</div>
</div>
<div class="content site-width">
<h2>Building Blocks</h2>
<div class="cols">
<div>
<h3 class="headline-link"><a href="//amphp.org/amp/event-loop">Event Loop<i class="fa fa-angle-right"></i></a></h3>
<p>
The event loop is the main task scheduler of every asynchronous application. It dispatches
associ­ated handlers once the registered events happen.
</p>
</div>
<div>
<h3 class="headline-link"><a href="//amphp.org/amp/promises">Promises<i class="fa fa-angle-right"></i></a></h3>
<p>
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.
</p>
</div>
<div>
<h3 class="headline-link"><a href="//amphp.org/amp/coroutines">Coroutines<i class="fa fa-angle-right"></i></a></h3>
<p>
Coroutines are interruptible functions that can be paused and resumed. Amp uses
<code>Generator</code>s
to allow promise consumption without callbacks.
</p>
</div>
</div>
<div class="cols">
<div>
<h3 class="headline-link"><a href="//amphp.org/amp/iterators">Iterators<i class="fa fa-angle-right"></i></a></h3>
<p>
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.
</p>
</div>
<div>
<h3 class="headline-link"><a href="//amphp.org/byte-stream">Streams<i class="fa fa-angle-right"></i></a></h3>
<p>
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.
</p>
</div>
</div>
</div>
<div class="content site-width">
<h2>Use Cases</h2>
<div>
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 <a href="/amp/promises/miscellaneous#wait"><code>Amp\Promise\wait()</code></a> helpful.
</div>
<div class="cols">
<div>
<h3>Multiplexing I/O</h3>
<p>
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 <a href="/artax/">parallel HTTP requests</a>, <a href="https://github.com/amphp/mysql">SQL queries</a> or any other I/O related activity.
</p>
</div>
<div>
<h3>Reacting to I/O</h3>
<p>
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 <a href="/amp/event-loop/">event loop</a> provides exactly that. It lets you register callbacks to react to I/O events.
</p>
</div>
</div>
</div>
<div class="content site-width">
<h2>Compatible Packages</h2>
<p>
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 <a
href="https://github.com/search?p=1&q=topic%3Aamphp&type=Repositories&utf8=%E2%9C%93"><code>amphp</code></a>
tag on GitHub. Many packages are listed on our dedicated <a href="/packages">Packages</a> page.
</p>
<p>
<a href="//amphp.org/react-adapter">Every ReactPHP library is compatible with Amp</a>, so you don't need to choose between those two.
</p>
</div>