-
-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathprocess.php
executable file
·37 lines (28 loc) · 1.2 KB
/
process.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/env php
<?php
require dirname(__DIR__).'/vendor/autoload.php';
use Amp\ByteStream;
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.
$context = yield Process::run(__DIR__ . "/blocking-process.php");
assert($context instanceof Process);
// Pipe any data written to the STDOUT in the child process to STDOUT of this process.
Amp\Promise\rethrow(ByteStream\pipe($context->getStdout(), ByteStream\getStdout()));
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
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());
} finally {
Loop::cancel($timer);
}
});