Skip to content

Commit 5944b2d

Browse files
committed
Documentation for event loop implementations
1 parent 9a6c2b9 commit 5944b2d

File tree

6 files changed

+118
-52
lines changed

6 files changed

+118
-52
lines changed

README.md

+90-46
Original file line numberDiff line numberDiff line change
@@ -18,19 +18,25 @@ For the code of the current stable 0.4.x release, checkout the
1818

1919
* [Quickstart example](#quickstart-example)
2020
* [Usage](#usage)
21-
* [Factory](#factory)
22-
* [Loop implementations](#loop-implementations)
23-
* [addtimer()](#addtimer)
24-
* [addPeriodicTimer()](#addperiodictimer)
25-
* [cancelTimer()](#canceltimer)
26-
* [isTimerActive()](#istimeractive)
27-
* [futureTick()](#futuretick)
28-
* [addSignal()](#addsignal)
29-
* [removeSignal()](#removesignal)
30-
* [addReadStream()](#addreadstream)
31-
* [addWriteStream()](#addwritestream)
32-
* [removeReadStream()](#removereadstream)
33-
* [removeWriteStream()](#removewritestream)
21+
* [Factory](#factory)
22+
* [create()](#create)
23+
* [Loop implementations](#loop-implementations)
24+
* [StreamSelectLoop](#streamselectloop)
25+
* [LibEventLoop](#libeventloop)
26+
* [LibEvLoop](#libevloop)
27+
* [ExtEventLoop](#exteventloop)
28+
* [LoopInterface](#loopinterface)
29+
* [addtimer()](#addtimer)
30+
* [addPeriodicTimer()](#addperiodictimer)
31+
* [cancelTimer()](#canceltimer)
32+
* [isTimerActive()](#istimeractive)
33+
* [futureTick()](#futuretick)
34+
* [addSignal()](#addsignal)
35+
* [removeSignal()](#removesignal)
36+
* [addReadStream()](#addreadstream)
37+
* [addWriteStream()](#addwritestream)
38+
* [removeReadStream()](#removereadstream)
39+
* [removeWriteStream()](#removewritestream)
3440
* [Install](#install)
3541
* [Tests](#tests)
3642
* [License](#license)
@@ -106,49 +112,79 @@ $loop->run();
106112
purposes.
107113
3. The loop is run with a single `$loop->run()` call at the end of the program.
108114

109-
## Factory
115+
### Factory
110116

111117
The `Factory` class exists as a convenient way to pick the best available
112-
[loop implementation](#loop-implementations).
118+
[event loop implementation](#loop-implementations).
113119

114-
The `create(): LoopInterface` method can be used to create a new loop
120+
#### create()
121+
122+
The `create(): LoopInterface` method can be used to create a new event loop
115123
instance:
116124

117125
```php
118126
$loop = React\EventLoop\Factory::create();
119127
```
120128

121-
This method always returns an instance implementing `LoopInterface`,
122-
the actual loop implementation is an implementation detail.
129+
This method always returns an instance implementing [`LoopInterface`](#loopinterface),
130+
the actual [event loop implementation](#loop-implementations) is an implementation detail.
123131

124132
This method should usually only be called once at the beginning of the program.
125133

126-
## Loop implementations
127-
128-
In addition to the interface there are the following implementations provided:
129-
130-
* `StreamSelectLoop`: This is the only implementation which works out of the
131-
box with PHP. It does a simple `select` system call. It's not the most
132-
performant of loops, but still does the job quite well.
134+
### Loop implementations
133135

134-
* `LibEventLoop`: This uses the `libevent` pecl extension. `libevent` itself
135-
supports a number of system-specific backends (epoll, kqueue).
136+
In addition to the [`LoopInterface`](#loopinterface), there are a number of
137+
event loop implementations provided.
136138

137-
* `LibEvLoop`: This uses the `libev` pecl extension
138-
([github](https://github.com/m4rw3r/php-libev)). It supports the same
139-
backends as libevent.
140-
141-
* `ExtEventLoop`: This uses the `event` pecl extension. It supports the same
142-
backends as libevent.
143-
144-
All of the loops support these features:
139+
All of the event loops support these features:
145140

146141
* File descriptor polling
147142
* One-off timers
148143
* Periodic timers
149144
* Deferred execution on future loop tick
150145

151-
### addTimer()
146+
For most consumers of this package, the underlying event loop implementation is
147+
an implementation detail.
148+
You should use the [`Factory`](#factory) to automatically create a new instance.
149+
150+
Advanced! If you explicitly need a certain event loop implementation, you can
151+
manually instantiate one of the following classes.
152+
Note that you may have to install the required PHP extensions for the respective
153+
event loop implementation first or this may result in a fatal error.
154+
155+
#### StreamSelectLoop
156+
157+
A `stream_select()` based event loop.
158+
159+
This uses the [`stream_select()`](http://php.net/manual/en/function.stream-select.php)
160+
function and is the only implementation which works out of the box with PHP.
161+
It does a simple `select` system call.
162+
It's not the most performant of loops, but still does the job quite well.
163+
164+
#### LibEventLoop
165+
166+
An `ext-libevent` based event loop.
167+
168+
This uses the [`libevent` PECL extension](https://pecl.php.net/package/libevent).
169+
`libevent` itself supports a number of system-specific backends (epoll, kqueue).
170+
171+
#### LibEvLoop
172+
173+
An `ext-libev` based event loop.
174+
175+
This uses an [unofficial `libev` extension](https://github.com/m4rw3r/php-libev).
176+
It supports the same backends as libevent.
177+
178+
#### ExtEventLoop
179+
180+
An `ext-event` based event loop.
181+
182+
This uses the [`event` PECL extension](https://pecl.php.net/package/event).
183+
It supports the same backends as libevent.
184+
185+
### LoopInterface
186+
187+
#### addTimer()
152188

153189
The `addTimer(float $interval, callable $callback): TimerInterface` method can be used to
154190
enqueue a callback to be invoked once after the given interval.
@@ -195,7 +231,7 @@ hello('Tester', $loop);
195231
The execution order of timers scheduled to execute at the same time is
196232
not guaranteed.
197233

198-
### addPeriodicTimer()
234+
#### addPeriodicTimer()
199235

200236
The `addPeriodicTimer(float $interval, callable $callback): TimerInterface` method can be used to
201237
enqueue a callback to be invoked repeatedly after the given interval.
@@ -249,7 +285,7 @@ hello('Tester', $loop);
249285
The execution order of timers scheduled to execute at the same time is
250286
not guaranteed.
251287

252-
### cancelTimer()
288+
#### cancelTimer()
253289

254290
The `cancelTimer(TimerInterface $timer): void` method can be used to
255291
cancel a pending timer.
@@ -264,7 +300,7 @@ Calling this method on a timer instance that has not been added to this
264300
loop instance or on a timer that is not "active" (or has already been
265301
cancelled) has no effect.
266302

267-
### isTimerActive()
303+
#### isTimerActive()
268304

269305
The `isTimerActive(TimerInterface $timer): bool` method can be used to
270306
check if a given timer is active.
@@ -274,7 +310,7 @@ via [`addTimer()`](#addtimer) or [`addPeriodicTimer()`](#addperiodictimer)
274310
and has not been cancelled via [`cancelTimer()`](#canceltimer) and is not
275311
a non-periodic timer that has already been triggered after its interval.
276312

277-
### futureTick()
313+
#### futureTick()
278314

279315
The `futureTick(callable $listener): void` method can be used to
280316
schedule a callback to be invoked on a future tick of the event loop.
@@ -322,7 +358,7 @@ echo 'a';
322358

323359
See also [example #3](examples).
324360

325-
### addSignal()
361+
#### addSignal()
326362

327363
The `addSignal(int $signal, callable $listener): void` method can be used to
328364
register a listener to be notified when a signal has been caught by this process.
@@ -356,7 +392,7 @@ missing.
356392
**Note: A listener can only be added once to the same signal, any
357393
attempts to add it more then once will be ignored.**
358394

359-
### removeSignal()
395+
#### removeSignal()
360396

361397
The `removeSignal(int $signal, callable $listener): void` method can be used to
362398
remove a previously added signal listener.
@@ -367,7 +403,7 @@ $loop->removeSignal(SIGINT, $listener);
367403

368404
Any attempts to remove listeners that aren't registered will be ignored.
369405

370-
### addReadStream()
406+
#### addReadStream()
371407

372408
> Advanced! Note that this low-level API is considered advanced usage.
373409
Most use cases should probably use the higher-level
@@ -410,7 +446,7 @@ read event listener for this stream.
410446
The execution order of listeners when multiple streams become ready at
411447
the same time is not guaranteed.
412448

413-
### addWriteStream()
449+
#### addWriteStream()
414450

415451
> Advanced! Note that this low-level API is considered advanced usage.
416452
Most use cases should probably use the higher-level
@@ -453,15 +489,15 @@ write event listener for this stream.
453489
The execution order of listeners when multiple streams become ready at
454490
the same time is not guaranteed.
455491

456-
### removeReadStream()
492+
#### removeReadStream()
457493

458494
The `removeReadStream(resource $stream): void` method can be used to
459495
remove the read event listener for the given stream.
460496

461497
Removing a stream from the loop that has already been removed or trying
462498
to remove a stream that was never added or is invalid has no effect.
463499

464-
### removeWriteStream()
500+
#### removeWriteStream()
465501

466502
The `removeWriteStream(resource $stream): void` method can be used to
467503
remove the write event listener for the given stream.
@@ -480,6 +516,14 @@ This will install the latest supported version:
480516
$ composer require react/event-loop
481517
```
482518

519+
This project aims to run on any platform and thus does not require any PHP
520+
extensions and supports running on legacy PHP 5.4 through current PHP 7+ and
521+
HHVM.
522+
It's *highly recommended to use PHP 7+* for this project.
523+
524+
Installing any of the event loop extensions is suggested, but entirely optional.
525+
See also [event loop implementations](#loop-implementations) for more details.
526+
483527
## Tests
484528

485529
To run the test suite, you first need to clone this repo and then install all

src/ExtEventLoop.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,12 @@
1111
use SplObjectStorage;
1212

1313
/**
14-
* An ext-event based event-loop.
14+
* An `ext-event` based event loop.
15+
*
16+
* This uses the [`event` PECL extension](https://pecl.php.net/package/event).
17+
* It supports the same backends as libevent.
18+
*
19+
* @link https://pecl.php.net/package/event
1520
*/
1621
class ExtEventLoop implements LoopInterface
1722
{

src/Factory.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
namespace React\EventLoop;
44

55
/**
6-
* The `Factory` class exists as a convenient way to pick the best available loop implementation.
6+
* The `Factory` class exists as a convenient way to pick the best available event loop implementation.
77
*/
88
class Factory
99
{
1010
/**
11-
* Creates a new loop instance
11+
* Creates a new event loop instance
1212
*
1313
* ```php
1414
* $loop = React\EventLoop\Factory::create();
1515
* ```
1616
*
1717
* This method always returns an instance implementing `LoopInterface`,
18-
* the actual loop implementation is an implementation detail.
18+
* the actual event loop implementation is an implementation detail.
1919
*
2020
* This method should usually only be called once at the beginning of the program.
2121
*

src/LibEvLoop.php

+5
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@
1212
use SplObjectStorage;
1313

1414
/**
15+
* An `ext-libev` based event loop.
16+
*
17+
* This uses an [unofficial `libev` extension](https://github.com/m4rw3r/php-libev).
18+
* It supports the same backends as libevent.
19+
*
1520
* @see https://github.com/m4rw3r/php-libev
1621
* @see https://gist.github.com/1688204
1722
*/

src/LibEventLoop.php

+6-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@
1010
use SplObjectStorage;
1111

1212
/**
13-
* An ext-libevent based event-loop.
13+
* An `ext-libevent` based event loop.
14+
*
15+
* This uses the [`libevent` PECL extension](https://pecl.php.net/package/libevent).
16+
* `libevent` itself supports a number of system-specific backends (epoll, kqueue).
17+
*
18+
* @link https://pecl.php.net/package/libevent
1419
*/
1520
class LibEventLoop implements LoopInterface
1621
{

src/StreamSelectLoop.php

+8-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,14 @@
99
use React\EventLoop\Timer\Timers;
1010

1111
/**
12-
* A stream_select() based event-loop.
12+
* A `stream_select()` based event loop.
13+
*
14+
* This uses the [`stream_select()`](http://php.net/manual/en/function.stream-select.php)
15+
* function and is the only implementation which works out of the box with PHP.
16+
* It does a simple `select` system call.
17+
* It's not the most performant of loops, but still does the job quite well.
18+
*
19+
* @link http://php.net/manual/en/function.stream-select.php
1320
*/
1421
class StreamSelectLoop implements LoopInterface
1522
{

0 commit comments

Comments
 (0)