2015-08-07 01:59:25 +02:00
|
|
|
#!/usr/bin/env php
|
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
|
|
|
|
2016-08-23 23:47:40 +02:00
|
|
|
use Amp\Parallel\Forking\Fork;
|
2016-12-30 02:17:26 +01:00
|
|
|
use Interop\Async\Loop;
|
2015-07-09 08:35:34 +02:00
|
|
|
|
2016-12-30 02:17:26 +01:00
|
|
|
Loop::execute(Amp\wrap(function () {
|
2015-08-24 18:20:22 +02:00
|
|
|
$context = Fork::spawn(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
|
|
|
|
2016-08-18 18:04:48 +02:00
|
|
|
yield $this->send('Data sent from child.');
|
2015-08-07 22:30:14 +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-08-07 01:59:25 +02:00
|
|
|
|
2016-01-23 07:00:56 +01:00
|
|
|
return 42;
|
2015-07-12 22:57:04 +02:00
|
|
|
});
|
2015-08-07 22:30:14 +02:00
|
|
|
|
2016-12-30 02:17:26 +01:00
|
|
|
$timer = Loop::repeat(1000, function () use ($context) {
|
2015-07-12 22:57:04 +02:00
|
|
|
static $i;
|
2015-08-07 22:30:14 +02:00
|
|
|
$i = $i ? ++$i : 1;
|
2015-07-12 22:57:04 +02:00
|
|
|
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 {
|
2016-08-18 18:04:48 +02:00
|
|
|
printf("Received the following from child: %s\n", yield $context->receive());
|
|
|
|
printf("Child ended with value %d!\n", yield $context->join());
|
|
|
|
} catch (Throwable $e) {
|
2015-07-12 22:57:04 +02:00
|
|
|
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 {
|
2016-12-30 02:17:26 +01:00
|
|
|
Loop::cancel($timer);
|
2015-07-12 22:57:04 +02:00
|
|
|
}
|
2016-12-30 02:17:26 +01:00
|
|
|
}));
|