2015-07-09 08:35:34 +02:00
|
|
|
<?php
|
2015-08-05 20:29:11 +02:00
|
|
|
require dirname(__DIR__).'/vendor/autoload.php';
|
2015-07-09 08:35:34 +02:00
|
|
|
|
|
|
|
use Icicle\Concurrent\Forking\ForkContext;
|
2015-08-03 22:10:20 +02:00
|
|
|
use Icicle\Coroutine;
|
2015-07-12 22:57:04 +02:00
|
|
|
use Icicle\Loop;
|
2015-07-09 08:35:34 +02:00
|
|
|
|
2015-08-03 22:10:20 +02:00
|
|
|
Coroutine\create(function () {
|
2015-08-05 20:29:11 +02:00
|
|
|
$context = ForkContext::create(function () {
|
2015-07-13 19:30:00 +02:00
|
|
|
print "Child sleeping for 4 seconds...\n";
|
2015-07-12 22:57:04 +02:00
|
|
|
sleep(4);
|
2015-07-10 09:23:08 +02:00
|
|
|
|
2015-07-13 19:30:00 +02:00
|
|
|
print "Child sleeping for 2 seconds...\n";
|
2015-07-10 09:23:08 +02:00
|
|
|
sleep(2);
|
2015-07-12 22:57:04 +02:00
|
|
|
});
|
2015-07-27 00:53:00 +02:00
|
|
|
$context->start();
|
2015-07-10 09:23:08 +02:00
|
|
|
|
2015-07-12 22:57:04 +02:00
|
|
|
$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";
|
2015-07-10 09:23:08 +02:00
|
|
|
});
|
2015-07-09 08:35:34 +02:00
|
|
|
|
2015-07-12 22:57:04 +02:00
|
|
|
try {
|
|
|
|
yield $context->join();
|
|
|
|
print "Context done!\n";
|
|
|
|
} catch (Exception $e) {
|
|
|
|
print "Error from child!\n";
|
2015-08-05 20:29:11 +02:00
|
|
|
print $e."\n";
|
2015-07-12 22:57:04 +02:00
|
|
|
} finally {
|
|
|
|
$timer->stop();
|
|
|
|
}
|
2015-08-03 22:10:20 +02:00
|
|
|
});
|
2015-07-10 23:17:43 +02:00
|
|
|
|
2015-07-10 09:23:08 +02:00
|
|
|
Loop\run();
|