1
0
mirror of https://github.com/danog/process.git synced 2024-12-12 00:59:41 +01:00
process/examples/ping-many.php

34 lines
759 B
PHP
Raw Normal View History

<?php
include dirname(__DIR__) . "/vendor/autoload.php";
use Amp\Process\Process;
use Amp\Promise;
use function Amp\Promise\all;
2017-09-17 21:06:05 +02:00
function show_process_output(Process $process): \Generator
{
$stream = $process->getStdout();
while ($chunk = yield $stream->read()) {
echo $chunk;
}
$code = yield $process->join();
echo "Process {$process->getPid()} 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) {
2017-09-17 21:06:05 +02:00
$process = new Process("ping {$host}");
$process->start();
$promises[] = new \Amp\Coroutine(show_process_output($process));
}
yield all($promises);
});