1
0
mirror of https://github.com/danog/process.git synced 2024-11-26 20:24:43 +01:00
process/examples/ping-many.php
Aaron Piotrowski 91dc2ec100
Update examples
2018-10-15 09:55:15 -05:00

39 lines
884 B
PHP

<?php
include \dirname(__DIR__) . "/vendor/autoload.php";
use Amp\Process\Process;
use function Amp\Promise\all;
function show_process_output(Process $process): \Generator
{
yield $process->start();
$stream = $process->getStdout();
while (null !== $chunk = yield $stream->read()) {
echo $chunk;
}
$code = yield $process->join();
$pid = $process->getPid();
echo "Process {$pid} exited with {$code}\n";
}
Amp\Loop::run(function () {
$hosts = ['8.8.8.8', '8.8.4.4', 'google.com', 'stackoverflow.com', 'github.com'];
$promises = [];
foreach ($hosts as $host) {
$command = \DIRECTORY_SEPARATOR === "\\"
? "ping -n 5 {$host}"
: "ping -c 5 {$host}";
$process = new Process($command);
$promises[] = new Amp\Coroutine(show_process_output($process));
}
yield all($promises);
});