Skip to content

Commit 1d8546d

Browse files
committed
Separate README file for each component, fixes #11
1 parent 1e83d5c commit 1d8546d

1 file changed

Lines changed: 58 additions & 0 deletions

File tree

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# EventLoop Component
2+
3+
Event loop abstraction layer that libraries can use for evented I/O.
4+
5+
In order for async based libraries to be interoperable, they need to use the
6+
same event loop. This component provides a common `LoopInterface` that any
7+
library can target. This allows them to be used in the same loop, with one
8+
single `run` call that is controlled by the user.
9+
10+
In addition to the interface there are some implementations provided:
11+
12+
* `stream_select`: This is the only implementation which works out of the box
13+
with PHP. It does a simple `select` system call. It's not the most performant
14+
of loops, but still does the job quite well.
15+
16+
* `libevent`: This uses the `libevent` pecl extension. `libevent` itself
17+
supports a number of system-specific backends (epoll, kqueue).
18+
19+
All of the loops support these features:
20+
21+
* File descriptor polling
22+
* One-off timers
23+
* Periodic timers
24+
25+
## Usage
26+
27+
Here is an async HTTP server built with just the event loop.
28+
29+
$loop = React\EventLoop\Factory::create();
30+
31+
$server = stream_socket_server('tcp://127.0.0.1:8080');
32+
stream_set_blocking($server, 0);
33+
$loop->addReadStream($server, function ($server) use ($loop) {
34+
$conn = stream_socket_accept($server);
35+
$data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
36+
$loop->addWriteStream($conn, function ($conn) use (&$data, $loop) {
37+
$written = fwrite($conn, $data);
38+
if ($written === strlen($data)) {
39+
fclose($conn);
40+
$loop->removeStream($conn);
41+
} else {
42+
$data = substr($data, 0, $swritten);
43+
}
44+
});
45+
});
46+
47+
$loop->addPeriodicTimer(5, function () {
48+
$memory = memory_get_usage() / (1024*1024);
49+
$formatted = number_format($memory, 3).'K';
50+
echo "Current memory usage: {$formatted}\n";
51+
});
52+
53+
$loop->run();
54+
55+
**Note:** The factory is just for convenience. It tries to pick the best
56+
available implementation. Libraries `SHOULD` allow the user to inject an
57+
instance of the loop. They `MAY` use the factory when the user did not supply
58+
a loop.

0 commit comments

Comments
 (0)