2015-08-06 18:59:25 -05:00
|
|
|
#!/usr/bin/env php
|
2015-07-09 01:35:34 -05:00
|
|
|
<?php
|
2015-08-05 13:29:11 -05:00
|
|
|
require dirname(__DIR__).'/vendor/autoload.php';
|
2015-07-09 01:35:34 -05:00
|
|
|
|
2015-08-24 11:20:22 -05:00
|
|
|
use Icicle\Concurrent\Forking\Fork;
|
2015-08-03 15:10:20 -05:00
|
|
|
use Icicle\Coroutine;
|
2015-07-12 15:57:04 -05:00
|
|
|
use Icicle\Loop;
|
2015-07-09 01:35:34 -05:00
|
|
|
|
2015-08-03 15:10:20 -05:00
|
|
|
Coroutine\create(function () {
|
2015-08-24 11:20:22 -05:00
|
|
|
$context = Fork::spawn(function () {
|
2015-07-13 12:30:00 -05:00
|
|
|
print "Child sleeping for 4 seconds...\n";
|
2015-07-12 15:57:04 -05:00
|
|
|
sleep(4);
|
2015-07-10 02:23:08 -05:00
|
|
|
|
2015-08-07 15:30:14 -05:00
|
|
|
yield $this->send('Data sent from child.');
|
|
|
|
|
2015-07-13 12:30:00 -05:00
|
|
|
print "Child sleeping for 2 seconds...\n";
|
2015-07-10 02:23:08 -05:00
|
|
|
sleep(2);
|
2015-08-06 18:59:25 -05:00
|
|
|
|
2015-08-07 15:30:14 -05:00
|
|
|
yield 42;
|
2015-07-12 15:57:04 -05:00
|
|
|
});
|
2015-08-07 15:30:14 -05:00
|
|
|
|
2015-07-12 15:57:04 -05:00
|
|
|
$timer = Loop\periodic(1, function () use ($context) {
|
|
|
|
static $i;
|
2015-08-07 15:30:14 -05:00
|
|
|
$i = $i ? ++$i : 1;
|
2015-07-12 15:57:04 -05:00
|
|
|
print "Demonstrating how alive the parent is for the {$i}th time.\n";
|
2015-07-10 02:23:08 -05:00
|
|
|
});
|
2015-07-09 01:35:34 -05:00
|
|
|
|
2015-07-12 15:57:04 -05:00
|
|
|
try {
|
2015-08-07 15:30:14 -05:00
|
|
|
printf("Received the following from child: %s\n", (yield $context->receive()));
|
2015-08-06 18:59:25 -05:00
|
|
|
printf("Child ended with value %d!\n", (yield $context->join()));
|
2015-07-12 15:57:04 -05:00
|
|
|
} catch (Exception $e) {
|
|
|
|
print "Error from child!\n";
|
2015-08-05 13:29:11 -05:00
|
|
|
print $e."\n";
|
2015-07-12 15:57:04 -05:00
|
|
|
} finally {
|
|
|
|
$timer->stop();
|
|
|
|
}
|
2015-08-03 15:10:20 -05:00
|
|
|
});
|
2015-07-10 16:17:43 -05:00
|
|
|
|
2015-07-10 02:23:08 -05:00
|
|
|
Loop\run();
|