2017-09-14 19:34:18 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
include dirname(__DIR__) . "/vendor/autoload.php";
|
|
|
|
|
|
|
|
use Amp\Process\Process;
|
|
|
|
use function Amp\Promise\all;
|
|
|
|
|
2017-09-20 11:54:19 +02:00
|
|
|
function show_process_output(Process $process): \Generator {
|
2017-09-14 19:34:18 +02:00
|
|
|
$stream = $process->getStdout();
|
2017-09-17 19:07:13 +02:00
|
|
|
|
|
|
|
while (null !== $chunk = yield $stream->read()) {
|
2017-09-14 19:34:18 +02:00
|
|
|
echo $chunk;
|
|
|
|
}
|
|
|
|
|
|
|
|
$code = yield $process->join();
|
2017-09-17 19:07:13 +02:00
|
|
|
$pid = yield $process->getPid();
|
2017-09-14 19:34:18 +02:00
|
|
|
|
2017-09-17 19:07:13 +02:00
|
|
|
echo "Process {$pid} exited with {$code}\n";
|
2017-09-14 19:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Amp\Loop::run(function () {
|
|
|
|
$hosts = ['8.8.8.8', '8.8.4.4', 'google.com', 'stackoverflow.com', 'github.com'];
|
|
|
|
|
|
|
|
$promises = [];
|
|
|
|
|
|
|
|
foreach ($hosts as $host) {
|
2017-12-05 09:19:00 +01:00
|
|
|
$command = \DIRECTORY_SEPARATOR === "\\"
|
|
|
|
? "ping -n 5 {$host}"
|
2017-12-05 09:19:44 +01:00
|
|
|
: "ping -c 5 {$host}";
|
2017-12-05 09:19:00 +01:00
|
|
|
$process = new Process($command);
|
2017-09-17 19:07:13 +02:00
|
|
|
$process->start();
|
2017-11-25 01:50:25 +01:00
|
|
|
$promises[] = new Amp\Coroutine(show_process_output($process));
|
2017-09-14 19:34:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
yield all($promises);
|
|
|
|
});
|