2017-12-10 22:37:33 +01:00
|
|
|
#!/usr/bin/env php
|
|
|
|
<?php
|
2018-10-07 16:50:45 +02:00
|
|
|
require \dirname(__DIR__).'/vendor/autoload.php';
|
2017-12-10 22:37:33 +01:00
|
|
|
|
2018-10-21 17:34:08 +02:00
|
|
|
use Amp\ByteStream;
|
2017-12-10 22:37:33 +01:00
|
|
|
use Amp\Delayed;
|
|
|
|
use Amp\Loop;
|
|
|
|
use Amp\Parallel\Context\Process;
|
|
|
|
|
|
|
|
Loop::run(function () {
|
|
|
|
$timer = Loop::repeat(1000, function () {
|
|
|
|
static $i;
|
|
|
|
$i = $i ? ++$i : 1;
|
|
|
|
print "Demonstrating how alive the parent is for the {$i}th time.\n";
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
// Create a new child process that does some blocking stuff.
|
2018-10-06 17:05:49 +02:00
|
|
|
$context = yield Process::run(__DIR__ . "/blocking-process.php");
|
|
|
|
|
|
|
|
\assert($context instanceof Process);
|
2017-12-10 22:37:33 +01:00
|
|
|
|
2018-10-21 17:34:08 +02:00
|
|
|
// Pipe any data written to the STDOUT in the child process to STDOUT of this process.
|
2018-10-25 05:31:47 +02:00
|
|
|
Amp\Promise\rethrow(ByteStream\pipe($context->getStdout(), ByteStream\getStdout()));
|
2018-10-21 17:34:08 +02:00
|
|
|
|
2017-12-10 22:37:33 +01:00
|
|
|
print "Waiting 2 seconds to send start data...\n";
|
|
|
|
yield new Delayed(2000);
|
|
|
|
|
|
|
|
yield $context->send("Start data"); // Data sent to child process, received on line 9 of blocking-process.php
|
|
|
|
|
2018-10-07 16:50:45 +02:00
|
|
|
\printf("Received the following from child: %s\n", yield $context->receive()); // Sent on line 14 of blocking-process.php
|
|
|
|
\printf("Process ended with value %d!\n", yield $context->join());
|
2017-12-10 22:37:33 +01:00
|
|
|
} finally {
|
|
|
|
Loop::cancel($timer);
|
|
|
|
}
|
|
|
|
});
|