@@ -8,36 +8,22 @@ Event loop abstraction layer that libraries can use for evented I/O.
8
8
In order for async based libraries to be interoperable, they need to use the
9
9
same event loop. This component provides a common ` LoopInterface ` that any
10
10
library can target. This allows them to be used in the same loop, with one
11
- single ` run ` call that is controlled by the user.
11
+ single ` run() ` call that is controlled by the user.
12
12
13
13
> The master branch contains the code for the upcoming 0.5 release.
14
14
For the code of the current stable 0.4.x release, checkout the
15
15
[ 0.4 branch] ( https://github.com/reactphp/event-loop/tree/0.4 ) .
16
16
17
- In addition to the interface there are some implementations provided:
17
+ ** Table of Contents **
18
18
19
- * ` StreamSelectLoop ` : This is the only implementation which works out of the
20
- box with PHP. It does a simple ` select ` system call. It's not the most
21
- performant of loops, but still does the job quite well.
22
-
23
- * ` LibEventLoop ` : This uses the ` libevent ` pecl extension. ` libevent ` itself
24
- supports a number of system-specific backends (epoll, kqueue).
25
-
26
- * ` LibEvLoop ` : This uses the ` libev ` pecl extension
27
- ([ github] ( https://github.com/m4rw3r/php-libev ) ). It supports the same
28
- backends as libevent.
29
-
30
- * ` ExtEventLoop ` : This uses the ` event ` pecl extension. It supports the same
31
- backends as libevent.
32
-
33
- All of the loops support these features:
34
-
35
- * File descriptor polling
36
- * One-off timers
37
- * Periodic timers
38
- * Deferred execution of callbacks
19
+ * [ Quickstart example] ( #quickstart-example )
20
+ * [ Usage] ( #usage )
21
+ * [ Loop implementations] ( #loop-implementations )
22
+ * [ Install] ( #install )
23
+ * [ Tests] ( #tests )
24
+ * [ License] ( #license )
39
25
40
- ## Usage
26
+ ## Quickstart example
41
27
42
28
Here is an async HTTP server built with just the event loop.
43
29
@@ -46,6 +32,7 @@ $loop = React\EventLoop\Factory::create();
46
32
47
33
$server = stream_socket_server('tcp://127.0.0.1:8080');
48
34
stream_set_blocking($server, 0);
35
+
49
36
$loop->addReadStream($server, function ($server) use ($loop) {
50
37
$conn = stream_socket_accept($server);
51
38
$data = "HTTP/1.1 200 OK\r\nContent-Length: 3\r\n\r\nHi\n";
@@ -69,7 +56,92 @@ $loop->addPeriodicTimer(5, function () {
69
56
$loop->run();
70
57
```
71
58
72
- ** Note:** The factory is just for convenience. It tries to pick the best
73
- available implementation. Libraries ` SHOULD ` allow the user to inject an
74
- instance of the loop. They ` MAY ` use the factory when the user did not supply
75
- a loop.
59
+ ## Usage
60
+
61
+ Typical applications use a single event loop which is created at the beginning
62
+ and run at the end of the program.
63
+
64
+ ``` php
65
+ // [1]
66
+ $loop = React\EventLoop\Factory::create();
67
+
68
+ // [2]
69
+ $loop->addPeriodicTimer(1, function () {
70
+ echo "Tick\n";
71
+ });
72
+
73
+ $stream = new React\Stream\ReadableResourceStream(
74
+ fopen('file.txt', 'r'),
75
+ $loop
76
+ );
77
+
78
+ // [3]
79
+ $loop->run();
80
+ ```
81
+
82
+ 1 . The loop instance is created at the beginning of the program. A convenience
83
+ factory ` React\EventLoop\Factory::create() ` is provided by this library which
84
+ picks the best available [ loop implementation] ( #loop-implementations ) .
85
+ 2 . The loop instance is used directly or passed to library and application code.
86
+ In this example, a periodic timer is registered with the event loop which
87
+ simply outputs ` Tick ` every second and a
88
+ [ readable stream] ( https://github.com/reactphp/stream#readableresourcestream )
89
+ is created by using ReactPHP's
90
+ [ stream component] ( https://github.com/reactphp/stream ) for demonstration
91
+ purposes.
92
+ 3 . The loop is run with a single ` $loop->run() ` call at the end of the program.
93
+
94
+ ## Loop implementations
95
+
96
+ In addition to the interface there are the following implementations provided:
97
+
98
+ * ` StreamSelectLoop ` : This is the only implementation which works out of the
99
+ box with PHP. It does a simple ` select ` system call. It's not the most
100
+ performant of loops, but still does the job quite well.
101
+
102
+ * ` LibEventLoop ` : This uses the ` libevent ` pecl extension. ` libevent ` itself
103
+ supports a number of system-specific backends (epoll, kqueue).
104
+
105
+ * ` LibEvLoop ` : This uses the ` libev ` pecl extension
106
+ ([ github] ( https://github.com/m4rw3r/php-libev ) ). It supports the same
107
+ backends as libevent.
108
+
109
+ * ` ExtEventLoop ` : This uses the ` event ` pecl extension. It supports the same
110
+ backends as libevent.
111
+
112
+ All of the loops support these features:
113
+
114
+ * File descriptor polling
115
+ * One-off timers
116
+ * Periodic timers
117
+ * Deferred execution of callbacks
118
+
119
+ ## Install
120
+
121
+ The recommended way to install this library is [ through Composer] ( http://getcomposer.org ) .
122
+ [ New to Composer?] ( http://getcomposer.org/doc/00-intro.md )
123
+
124
+ This will install the latest supported version:
125
+
126
+ ``` bash
127
+ $ composer require react/event-loop
128
+ ```
129
+
130
+ ## Tests
131
+
132
+ To run the test suite, you first need to clone this repo and then install all
133
+ dependencies [ through Composer] ( http://getcomposer.org ) :
134
+
135
+ ``` bash
136
+ $ composer install
137
+ ```
138
+
139
+ To run the test suite, go to the project root and run:
140
+
141
+ ``` bash
142
+ $ php vendor/bin/phpunit
143
+ ```
144
+
145
+ ## License
146
+
147
+ MIT, see [ LICENSE file] ( LICENSE ) .
0 commit comments