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