1
0
mirror of https://github.com/danog/parallel.git synced 2025-01-22 22:11:11 +01:00
parallel/examples/fork.php
2015-08-03 15:10:20 -05:00

36 lines
808 B
PHP

<?php
require dirname(__DIR__) . '/vendor/autoload.php';
use Icicle\Concurrent\Forking\ForkContext;
use Icicle\Coroutine;
use Icicle\Loop;
Coroutine\create(function () {
$context = new ForkContext(function () {
print "Child sleeping for 4 seconds...\n";
sleep(4);
print "Child sleeping for 2 seconds...\n";
sleep(2);
});
$context->start();
$timer = Loop\periodic(1, function () use ($context) {
static $i;
$i = $i + 1 ?: 1;
print "Demonstrating how alive the parent is for the {$i}th time.\n";
});
try {
yield $context->join();
print "Context done!\n";
} catch (Exception $e) {
print "Error from child!\n";
print $e . "\n";
} finally {
$timer->stop();
}
});
Loop\run();