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

Fix examples and running on Unix

This commit is contained in:
Niklas Keller 2017-09-17 21:06:05 +02:00
parent 624d8b59de
commit 7117a6aa92
6 changed files with 15 additions and 12 deletions

View File

@ -6,7 +6,8 @@ use Amp\ByteStream\Message;
use Amp\Process\Process; use Amp\Process\Process;
Amp\Loop::run(function () { Amp\Loop::run(function () {
$process = yield Process::start("echo 'Hello, world!'"); $process = new Process("echo 'Hello, world!'");
$process->start();
echo yield new Message($process->getStdout()); echo yield new Message($process->getStdout());

View File

@ -6,11 +6,8 @@ use Amp\Process\Process;
use Amp\Promise; use Amp\Promise;
use function Amp\Promise\all; use function Amp\Promise\all;
function show_process_output(Promise $promise): \Generator function show_process_output(Process $process): \Generator
{ {
/** @var Process $process */
$process = yield $promise;
$stream = $process->getStdout(); $stream = $process->getStdout();
while ($chunk = yield $stream->read()) { while ($chunk = yield $stream->read()) {
echo $chunk; echo $chunk;
@ -27,7 +24,9 @@ Amp\Loop::run(function () {
$promises = []; $promises = [];
foreach ($hosts as $host) { foreach ($hosts as $host) {
$promises[] = new \Amp\Coroutine(show_process_output(Process::start("ping {$host}"))); $process = new Process("ping {$host}");
$process->start();
$promises[] = new \Amp\Coroutine(show_process_output($process));
} }
yield all($promises); yield all($promises);

View File

@ -5,10 +5,11 @@ include dirname(__DIR__) . "/vendor/autoload.php";
use Amp\Process\Process; use Amp\Process\Process;
Amp\Loop::run(function () { Amp\Loop::run(function () {
$process = yield Process::start("echo 1; sleep 1; echo 2; sleep 1; echo 3; exit 42"); $process = new Process("echo 1; sleep 1; echo 2; sleep 1; echo 3; exit 42");
$process->start();
$stream = $process->getStdout(); $stream = $process->getStdout();
while ($chunk = yield $stream->read()) { while (null !== $chunk = yield $stream->read()) {
echo $chunk; echo $chunk;
} }

View File

@ -6,7 +6,8 @@ use Amp\ByteStream\Message;
use Amp\Process\Process; use Amp\Process\Process;
Amp\Loop::run(function () { Amp\Loop::run(function () {
$process = yield Process::start('read ; echo "$REPLY"'); $process = new Process('read ; echo "$REPLY"');
$process->start();
/* send to stdin */ /* send to stdin */
$process->getStdin()->write("abc\n"); $process->getStdin()->write("abc\n");

View File

@ -24,6 +24,7 @@ final class Runner implements ProcessRunner {
public static function onProcessEndExtraDataPipeReadable($watcher, $stream, Handle $handle) { public static function onProcessEndExtraDataPipeReadable($watcher, $stream, Handle $handle) {
Loop::cancel($watcher); Loop::cancel($watcher);
$handle->extraDataPipeWatcher = null;
$handle->status = ProcessStatus::ENDED; $handle->status = ProcessStatus::ENDED;

View File

@ -200,7 +200,7 @@ class Process {
* @return ProcessOutputStream * @return ProcessOutputStream
*/ */
public function getStdin(): ProcessOutputStream { public function getStdin(): ProcessOutputStream {
return $this->stdin; return $this->handle->stdin;
} }
/** /**
@ -213,7 +213,7 @@ class Process {
throw new StatusError("The process is not running"); throw new StatusError("The process is not running");
} }
return $this->stdout; return $this->handle->stdout;
} }
/** /**
@ -226,7 +226,7 @@ class Process {
throw new StatusError("The process is not running"); throw new StatusError("The process is not running");
} }
return $this->stderr; return $this->handle->stderr;
} }
} }