Skip to content

Commit 78f7f43

Browse files
authored
Merge pull request #232 from clue-labs/loop-autorun
Automatically run Loop at end of program (autorun)
2 parents 81d17c1 + 9712eea commit 78f7f43

23 files changed

+247
-39
lines changed

README.md

Lines changed: 42 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ single [`run()`](#run) call that is controlled by the user.
1515
* [Usage](#usage)
1616
* [Loop](#loop)
1717
* [Loop methods](#loop-methods)
18+
* [Loop autorun](#loop-autorun)
1819
* [get()](#get)
1920
* [~~Factory~~](#factory)
2021
* [~~create()~~](#create)
@@ -76,8 +77,6 @@ Loop::addPeriodicTimer(5, function () {
7677
$formatted = number_format($memory, 3).'K';
7778
echo "Current memory usage: {$formatted}\n";
7879
});
79-
80-
Loop::run();
8180
```
8281

8382
See also the [examples](examples).
@@ -98,8 +97,6 @@ Loop::addTimer(1.0, function () use ($timer) {
9897
Loop::cancelTimer($timer);
9998
echo 'Done' . PHP_EOL;
10099
});
101-
102-
Loop::run();
103100
```
104101

105102
As an alternative, you can also explicitly create an event loop instance at the
@@ -127,12 +124,13 @@ In both cases, the program would perform the exact same steps.
127124
1. The event loop instance is created at the beginning of the program. This is
128125
implicitly done the first time you call the [`Loop` class](#loop) or
129126
explicitly when using the deprecated [`Factory::create() method`](#create)
130-
(or manually instantiating any of the [loop implementation](#loop-implementations)).
127+
(or manually instantiating any of the [loop implementations](#loop-implementations)).
131128
2. The event loop is used directly or passed as an instance to library and
132129
application code. In this example, a periodic timer is registered with the
133130
event loop which simply outputs `Tick` every fraction of a second until another
134131
timer stops the periodic timer after a second.
135-
3. The event loop is run at the end of the program with a single [`run()`](#run)
132+
3. The event loop is run at the end of the program. This is automatically done
133+
when using [`Loop` class](#loop) or explicitly with a single [`run()`](#run)
136134
call at the end of the program.
137135

138136
As of `v1.2.0`, we highly recommend using the [`Loop` class](#loop).
@@ -176,8 +174,6 @@ Loop::addTimer(1.0, function () use ($timer) {
176174
Loop::cancelTimer($timer);
177175
echo 'Done' . PHP_EOL;
178176
});
179-
180-
Loop::run();
181177
```
182178

183179
On the other hand, if you're familiar with object-oriented programming (OOP) and
@@ -208,14 +204,50 @@ class Greeter
208204
$greeter = new Greeter(Loop::get());
209205
$greeter->greet('Alice');
210206
$greeter->greet('Bob');
211-
212-
Loop::run();
213207
```
214208

215209
Each static method call will be forwarded as-is to the underlying event loop
216210
instance by using the [`Loop::get()`](#get) call internally.
217211
See [`LoopInterface`](#loopinterface) for more details about available methods.
218212

213+
#### Loop autorun
214+
215+
When using the `Loop` class, it will automatically execute the loop at the end of
216+
the program. This means the following example will schedule a timer and will
217+
automatically execute the program until the timer event fires:
218+
219+
```php
220+
use React\EventLoop\Loop;
221+
222+
Loop::addTimer(1.0, function () {
223+
echo 'Hello' . PHP_EOL;
224+
});
225+
```
226+
227+
As of `v1.2.0`, we highly recommend using the `Loop` class this way and omitting any
228+
explicit [`run()`](#run) calls. For BC reasons, the explicit [`run()`](#run)
229+
method is still valid and may still be useful in some applications, especially
230+
for a transition period towards the more concise style.
231+
232+
If you don't want the `Loop` to run automatically, you can either explicitly
233+
[`run()`](#run) or [`stop()`](#stop) it. This can be useful if you're using
234+
a global exception handler like this:
235+
236+
```php
237+
use React\EventLoop\Loop;
238+
239+
Loop::addTimer(10.0, function () {
240+
echo 'Never happens';
241+
});
242+
243+
set_exception_handler(function (Throwable $e) {
244+
echo 'Error: ' . $e->getMessage() . PHP_EOL;
245+
Loop::stop();
246+
});
247+
248+
throw new RuntimeException('Demo');
249+
```
250+
219251
#### get()
220252

221253
The `get(): LoopInterface` method can be used to
@@ -262,8 +294,6 @@ class Greeter
262294
$greeter = new Greeter(Loop::get());
263295
$greeter->greet('Alice');
264296
$greeter->greet('Bob');
265-
266-
Loop::run();
267297
```
268298

269299
See [`LoopInterface`](#loopinterface) for more details about available methods.

examples/01-timers.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@
1111
Loop::addTimer(0.3, function () {
1212
echo 'hello ';
1313
});
14-
15-
Loop::run();

examples/02-periodic.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,5 +12,3 @@
1212
Loop::cancelTimer($timer);
1313
echo 'Done' . PHP_EOL;
1414
});
15-
16-
Loop::run();

examples/03-ticks.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,3 @@
1111
echo 'c';
1212
});
1313
echo 'a';
14-
15-
Loop::run();

examples/04-signals.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,3 @@
1515
});
1616

1717
echo 'Listening for SIGINT. Use "kill -SIGINT ' . getmypid() . '" or CTRL+C' . PHP_EOL;
18-
19-
Loop::run();

examples/11-consume-stdin.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,3 @@
2424

2525
echo strlen($chunk) . ' bytes' . PHP_EOL;
2626
});
27-
28-
Loop::run();

examples/12-generate-yes.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,3 @@
3737
$data = substr($data, $r) . substr($data, 0, $r);
3838
}
3939
});
40-
41-
Loop::run();

examples/13-http-client-blocking.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,3 @@
2929

3030
echo $chunk;
3131
});
32-
33-
Loop::run();

examples/14-http-client-async.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,3 @@
5858
echo $chunk;
5959
});
6060
});
61-
62-
Loop::run();

examples/21-http-server.php

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,3 @@
3232
$formatted = number_format($memory, 3).'K';
3333
echo "Current memory usage: {$formatted}\n";
3434
});
35-
36-
Loop::run();

0 commit comments

Comments
 (0)