Skip to content

Commit ac541bf

Browse files
committed
Documentation and examples for deferred execution via futureTick()
1 parent 67d845d commit ac541bf

File tree

7 files changed

+126
-2
lines changed

7 files changed

+126
-2
lines changed

README.md

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,34 @@ All of the loops support these features:
114114
* File descriptor polling
115115
* One-off timers
116116
* Periodic timers
117-
* Deferred execution of callbacks
117+
* Deferred execution on future loop tick
118+
119+
### futureTick()
120+
121+
The `futureTick(callable $listener): void` method can be used to
122+
schedule a callback to be invoked on a future tick of the event loop.
123+
124+
This works very much similar to timers with an interval of zero seconds,
125+
but does not require the overhead of scheduling a timer queue.
126+
127+
Unlike timers, callbacks are guaranteed to be executed in the order they
128+
are enqueued.
129+
Also, once a callback is enqueued, there's no way to cancel this operation.
130+
131+
This is often used to break down bigger tasks into smaller steps (a form of
132+
cooperative multitasking).
133+
134+
```php
135+
$loop->futureTick(function () {
136+
echo 'b';
137+
});
138+
$loop->futureTick(function () {
139+
echo 'c';
140+
});
141+
echo 'a';
142+
```
143+
144+
See also [example #3](examples).
118145

119146
## Install
120147

examples/03-ticks.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
require __DIR__ . '/../vendor/autoload.php';
4+
5+
$loop = React\EventLoop\Factory::create();
6+
7+
$loop->futureTick(function () {
8+
echo 'b';
9+
});
10+
$loop->futureTick(function () {
11+
echo 'c';
12+
});
13+
echo 'a';
14+
15+
$loop->run();

examples/91-benchmark-ticks.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$loop = Factory::create();
8+
9+
$n = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
10+
11+
for ($i = 0; $i < $n; ++$i) {
12+
$loop->nextTick(function () { });
13+
}
14+
15+
$loop->run();

examples/92-benchmark-timers.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$loop = Factory::create();
8+
9+
$n = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
10+
11+
for ($i = 0; $i < $n; ++$i) {
12+
$loop->addTimer(0, function () { });
13+
}
14+
15+
$loop->run();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$loop = Factory::create();
8+
9+
$ticks = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
10+
$tick = function () use (&$tick, &$ticks, $loop) {
11+
if ($ticks > 0) {
12+
--$ticks;
13+
//$loop->addTimer(0, $tick);
14+
$loop->nextTick($tick);
15+
} else {
16+
echo 'done';
17+
}
18+
};
19+
20+
$tick();
21+
22+
$loop->run();
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
use React\EventLoop\Factory;
4+
5+
require __DIR__ . '/../vendor/autoload.php';
6+
7+
$loop = Factory::create();
8+
9+
$ticks = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
10+
$tick = function () use (&$tick, &$ticks, $loop) {
11+
if ($ticks > 0) {
12+
--$ticks;
13+
//$loop->nextTick($tick);
14+
$loop->addTimer(0, $tick);
15+
} else {
16+
echo 'done';
17+
}
18+
};
19+
20+
$tick();
21+
22+
$loop->run();

src/LoopInterface.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,15 @@ public function isTimerActive(TimerInterface $timer);
8888
/**
8989
* Schedule a callback to be invoked on a future tick of the event loop.
9090
*
91-
* Callbacks are guaranteed to be executed in the order they are enqueued.
91+
* This works very much similar to timers with an interval of zero seconds,
92+
* but does not require the overhead of scheduling a timer queue.
93+
*
94+
* Unlike timers, callbacks are guaranteed to be executed in the order they
95+
* are enqueued.
96+
* Also, once a callback is enqueued, there's no way to cancel this operation.
97+
*
98+
* This is often used to break down bigger tasks into smaller steps (a form of
99+
* cooperative multitasking).
92100
*
93101
* @param callable $listener The callback to invoke.
94102
*/

0 commit comments

Comments
 (0)