8000 Documentation for run() and stop() · reactphp/event-loop@43a0add · GitHub
[go: up one dir, main page]

Skip to content

Commit 43a0add

Browse files
committed
Documentation for run() and stop()
1 parent 84de5da commit 43a0add

File tree

2 files changed

+97
-3
lines changed

2 files changed

+97
-3
lines changed

README.md

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Event loop abstraction layer that libraries can use for evented I/O.
88
In order for async based libraries to be interoperable, they need to use the
99
same event loop. This component provides a common `LoopInterface` that any
1010
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()`](#run) call that is controlled by the user.
1212

1313
> The master branch contains the code for the upcoming 0.5 release.
1414
For the code of the current stable 0.4.x release, checkout the
@@ -26,6 +26,8 @@ For the code of the current stable 0.4.x release, checkout the
2626
* [ExtLibeventLoop](#extlibeventloop)
2727
* [ExtLibevLoop](#extlibevloop)
2828
* [LoopInterface](#loopinterface)
29+
* [run()](#run)
30+
* [stop()](#stop)
2931
* [addTimer()](#addtimer)
3032
* [addPeriodicTimer()](#addperiodictimer)
3133
* [cancelTimer()](#canceltimer)
@@ -100,7 +102,7 @@ $loop->run();
100102
```
101103

102104
1. The loop instance is created at the beginning of the program. A convenience
103-
factory `React\EventLoop\Factory::create()` is provided by this library which
105+
factory [`React\EventLoop\Factory::create()`](#create) is provided by this library which
104106
picks the best available [loop implementation](#loop-implementations).
105107
2. The loop instance is used directly or passed to library and application code.
106108
In this example, a periodic timer is registered with the event loop which
@@ -109,7 +111,7 @@ $loop->run();
109111
is created by using ReactPHP's
110112
[stream component](https://github.com/reactphp/stream) for demonstration
111113
purposes.
112-
3. The loop is run with a single `$loop->run()` call at the end of the program.
114+
3. The loop is run with a single [`$loop->run()`](#run) call at the end of the program.
113115

114116
### Factory
115117

@@ -237,6 +239,55 @@ to happen any time soon.
237239

238240
### LoopInterface
239241

242+
#### run()
243+
244+
The `run(): void` method can be used to
245+
run the event loop until there are no more tasks to perform.
246+
247+
For many applications, this method is the only directly visible
248+
invocation on the event loop.
249+
As a rule of thumb, it is usally recommended to attach everything to the
250+
same loop instance and then run the loop once at the bottom end of the
251+
application.
252+
253+
```php
254+
$loop->run();
255+
```
256+
257+
This method will keep the loop running until there are no more tasks
258+
to perform. In other words: This method will block until the last
259+
timer, stream and/or signal has been removed.
260+
261+
Likewise, it is imperative to ensure the application actually invokes
262+
this method once. Adding listeners to the loop and missing to actually
263+
run it will result in the application exiting without actually waiting
264+
for any of the attached listeners.
265+
266+
This method MUST NOT be called while the loop is already running.
267+
This method MAY be called more than once after it has explicity been
268+
[`stop()`ped](#stop) or after it automatically stopped because it
269+
previously did no longer have anything to do.
270+
271+
#### stop()
272+
273+
The `stop(): void` method can be used to
274+
instruct a running event loop to stop.
275+
276+
This method is considered advanced usage and should be used with care.
277+
As a rule of thumb, it is usually recommended to let the loop stop
278+
only automatically when it no longer has anything to do.
279+
280+
This method can be used to explicitly instruct the event loop to stop:
281+
282+
```php
283+
$loop->addTimer(3.0, function () use ($loop) {
284+
$loop->stop();
285+
});
286+
```
287+
288+
Calling this method on a loop instance that is not currently running or
289+
on a loop instance that has already been stopped has no effect.
290+
240291
#### addTimer()
241292

242293
The `addTimer(float $interval, callable $callback): TimerInterface` method can be used to

src/LoopInterface.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -410,11 +410,54 @@ public function removeSignal($signal, $listener);
410410

411411
/**
412412
* Run the event loop until there are no more tasks to perform.
413+
*
414+
* For many applications, this method is the only directly visible
415+
* invocation on the event loop.
416+
* As a rule of thumb, it is usally recommended to attach everything to the
417+
* same loop instance and then run the loop once at the bottom end of the
418+
* application.
419+
*
420+
* ```php
421+
* $loop->run();
422+
* ```
423+
*
424+
* This method will keep the loop running until there are no more tasks
425+
* to perform. In other words: This method will block until the last
426+
* timer, stream and/or signal has been removed.
427+
*
428+
* Likewise, it is imperative to ensure the application actually invokes
429+
* this method once. Adding listeners to the loop and missing to actually
430+
* run it will result in the application exiting without actually waiting
431+
* for any of the attached listeners.
432+
*
433+
* This method MUST NOT be called while the loop is already running.
434+
* This method MAY be called more than once after it has explicity been
435+
* [`stop()`ped](#stop) or after it automatically stopped because it
436+
* previously did no longer have anything to do.
437+
*
438+
* @return void
413439
*/
414440
public function run();
415441

416442
/**
417443
* Instruct a running event loop to stop.
444+
*
445+
* This method is considered advanced usage and should be used with care.
446+
* As a rule of thumb, it is usually recommended to let the loop stop
447+
* only automatically when it no longer has anything to do.
448+
*
449+
* This method can be used to explicitly instruct the event loop to stop:
450+
*
451+
* ```php
452+
* $loop->addTimer(3.0, function () use ($loop) {
453+
* $loop->stop();
454+
* });
455+
* ```
456+
*
457+
* Calling this method on a loop instance that is not currently running or
458+
* on a loop instance that has already been stopped has no effect.
459+
*
460+
* @return void
418461
*/
419462
public function stop();
420463
}

0 commit comments

Comments
 (0)
0