mirror of
https://github.com/danog/process.git
synced 2024-11-30 04:39:04 +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:
parent
b2126ce56d
commit
4a7eb21797
@ -8,18 +8,6 @@ use Concurrent\Task;
|
||||
|
||||
$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'];
|
||||
|
||||
foreach ($hosts as $host) {
|
||||
@ -30,7 +18,5 @@ foreach ($hosts as $host) {
|
||||
$process = new Process($command);
|
||||
$process->start();
|
||||
|
||||
Task::async(function () use ($process) {
|
||||
show_process_output($process);
|
||||
});
|
||||
Task::async('Amp\ByteStream\pipe', $process->getStdout(), $stdout);
|
||||
}
|
@ -19,10 +19,10 @@ class Process
|
||||
private $command;
|
||||
|
||||
/** @var string */
|
||||
private $cwd = "";
|
||||
private $cwd;
|
||||
|
||||
/** @var array */
|
||||
private $env = [];
|
||||
private $env;
|
||||
|
||||
/** @var array */
|
||||
private $options;
|
||||
@ -93,7 +93,7 @@ class Process
|
||||
* @throws StatusError If the process has already been started.
|
||||
* @throws ProcessException If starting the process fails.
|
||||
*/
|
||||
public function start()
|
||||
public function start(): void
|
||||
{
|
||||
if ($this->handle) {
|
||||
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.
|
||||
*
|
||||
* @throws StatusError If the process has already been started.
|
||||
* @throws ProcessException If the process is killed.
|
||||
*/
|
||||
public function join(): int
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user