1
0
mirror of https://github.com/danog/process.git synced 2024-12-02 09:37:55 +01:00

Simplify example

This currently fails to execute correctly, because processes are killed if the reference is garbage collected. A later commit will fix this.
This commit is contained in:
Niklas Keller 2018-07-20 20:57:44 +02:00
parent b2126ce56d
commit 4a7eb21797
2 changed files with 5 additions and 18 deletions

View File

@ -8,18 +8,6 @@ use Concurrent\Task;
$stdout = new ResourceOutputStream(STDOUT); $stdout = new ResourceOutputStream(STDOUT);
function show_process_output(Process $process): void
{
global $stdout;
Amp\ByteStream\pipe($process->getStdout(), $stdout);
$pid = $process->getPid();
$exitCode = $process->join();
$stdout->write("Process {$pid} exited with {$exitCode}" . PHP_EOL);
}
$hosts = ['8.8.8.8', '8.8.4.4', 'google.com', 'stackoverflow.com', 'github.com']; $hosts = ['8.8.8.8', '8.8.4.4', 'google.com', 'stackoverflow.com', 'github.com'];
foreach ($hosts as $host) { foreach ($hosts as $host) {
@ -30,7 +18,5 @@ foreach ($hosts as $host) {
$process = new Process($command); $process = new Process($command);
$process->start(); $process->start();
Task::async(function () use ($process) { Task::async('Amp\ByteStream\pipe', $process->getStdout(), $stdout);
show_process_output($process);
});
} }

View File

@ -19,10 +19,10 @@ class Process
private $command; private $command;
/** @var string */ /** @var string */
private $cwd = ""; private $cwd;
/** @var array */ /** @var array */
private $env = []; private $env;
/** @var array */ /** @var array */
private $options; private $options;
@ -93,7 +93,7 @@ class Process
* @throws StatusError If the process has already been started. * @throws StatusError If the process has already been started.
* @throws ProcessException If starting the process fails. * @throws ProcessException If starting the process fails.
*/ */
public function start() public function start(): void
{ {
if ($this->handle) { if ($this->handle) {
throw new StatusError("Process has already been started."); throw new StatusError("Process has already been started.");
@ -108,6 +108,7 @@ class Process
* @return int Succeeds with process exit code or fails with a ProcessException if the process is killed. * @return int Succeeds with process exit code or fails with a ProcessException if the process is killed.
* *
* @throws StatusError If the process has already been started. * @throws StatusError If the process has already been started.
* @throws ProcessException If the process is killed.
*/ */
public function join(): int public function join(): int
{ {