1
0
mirror of https://github.com/danog/process.git synced 2024-11-30 04:39:04 +01:00
process/examples/ping-many.php
2021-12-14 00:32:16 +01:00

35 lines
747 B
PHP

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