Skip to content

Documentation and examples for deferred execution via futureTick() #100

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 25, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,34 @@ All of the loops support these features:
* File descriptor polling
* One-off timers
* Periodic timers
* Deferred execution of callbacks
* Deferred execution on future loop tick

### futureTick()

The `futureTick(callable $listener): void` method can be used to
schedule a callback to be invoked on a future tick of the event loop.

This works very much similar to timers with an interval of zero seconds,
but does not require the overhead of scheduling a timer queue.

Unlike timers, callbacks are guaranteed to be executed in the order they
are enqueued.
Also, once a callback is enqueued, there's no way to cancel this operation.

This is often used to break down bigger tasks into smaller steps (a form of
cooperative multitasking).

```php
$loop->futureTick(function () {
echo 'b';
});
$loop->futureTick(function () {
echo 'c';
});
echo 'a';
```

See also [example #3](examples).

## Install

Expand Down
15 changes: 15 additions & 0 deletions examples/03-ticks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

require __DIR__ . '/../vendor/autoload.php';

$loop = React\EventLoop\Factory::create();

$loop->futureTick(function () {
echo 'b';
});
$loop->futureTick(function () {
echo 'c';
});
echo 'a';

$loop->run();
15 changes: 15 additions & 0 deletions examples/91-benchmark-ticks.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use React\EventLoop\Factory;

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();

$n = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;

for ($i = 0; $i < $n; ++$i) {
$loop->nextTick(function () { });
}

$loop->run();
15 changes: 15 additions & 0 deletions examples/92-benchmark-timers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

use React\EventLoop\Factory;

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();

$n = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;

for ($i = 0; $i < $n; ++$i) {
$loop->addTimer(0, function () { });
}

$loop->run();
22 changes: 22 additions & 0 deletions examples/93-benchmark-ticks-delay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use React\EventLoop\Factory;

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();

$ticks = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
$tick = function () use (&$tick, &$ticks, $loop) {
if ($ticks > 0) {
--$ticks;
//$loop->addTimer(0, $tick);
$loop->nextTick($tick);
} else {
echo 'done';
}
};

$tick();

$loop->run();
22 changes: 22 additions & 0 deletions examples/94-benchmark-timers-delay.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

use React\EventLoop\Factory;

require __DIR__ . '/../vendor/autoload.php';

$loop = Factory::create();

$ticks = isset($argv[1]) ? (int)$argv[1] : 1000 * 100;
$tick = function () use (&$tick, &$ticks, $loop) {
if ($ticks > 0) {
--$ticks;
//$loop->nextTick($tick);
$loop->addTimer(0, $tick);
} else {
echo 'done';
}
};

$tick();

$loop->run();
10 changes: 9 additions & 1 deletion src/LoopInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,15 @@ public function isTimerActive(TimerInterface $timer);
/**
* Schedule a callback to be invoked on a future tick of the event loop.
*
* Callbacks are guaranteed to be executed in the order they are enqueued.
* This works very much similar to timers with an interval of zero seconds,
* but does not require the overhead of scheduling a timer queue.
*
* Unlike timers, callbacks are guaranteed to be executed in the order they
* are enqueued.
* Also, once a callback is enqueued, there's no way to cancel this operation.
*
* This is often used to break down bigger tasks into smaller steps (a form of
* cooperative multitasking).
*
* @param callable $listener The callback to invoke.
*/
Expand Down