Skip to content

Commit 8a009ff

Browse files
committed
Merge remote-tracking branch 'origin/2.1' into 2.1
2 parents 66ed966 + 66b685a commit 8a009ff

File tree

1 file changed

+39
-3
lines changed

1 file changed

+39
-3
lines changed

components/process.rst

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,16 @@ a command in a sub-process::
2626
$process = new Process('ls -lsa');
2727
$process->setTimeout(3600);
2828
$process->run();
29+
30+
// executes after the the command finishes
2931
if (!$process->isSuccessful()) {
3032
throw new \RuntimeException($process->getErrorOutput());
3133
}
3234

3335
print $process->getOutput();
3436

35-
The :method:`Symfony\\Component\\Process\\Process::run` method takes care
36-
of the subtle differences between the different platforms when executing the
37-
command.
37+
The component takes care of the subtle differences between the different platforms
38+
when executing the command.
3839

3940
When executing a long running command (like rsync-ing files to a remote
4041
server), you can give feedback to the end user in real-time by passing an
@@ -51,6 +52,41 @@ anonymous function to the
5152
echo 'OUT > '.$buffer;
5253
}
5354
});
55+
56+
.. versionadded:: 2.1
57+
The non-blocking feature was added in 2.1.
58+
59+
You can also start the subprocess and then let it run asynchronously, retrieving
60+
output and the status in your main process whenever you need it. Use the
61+
:method:`Symfony\\Component\\Process\\Process::start` method to start an asynchronous
62+
process, the :method:`Symfony\\Component\\Process\\Process::isRunning` method
63+
to check if the process is done and the
64+
:method:`Symfony\\Component\\Process\\Process::getOutput` method to get the output::
65+
66+
$process = new Process('ls -lsa');
67+
$process->start();
68+
69+
while ($process->isRunning()) {
70+
// waiting for process to finish
71+
}
72+
73+
echo $process->getOutput();
74+
75+
You can also wait for a process to end if you started it asynchronously and
76+
are done doing other stuff::
77+
78+
$process = new Process('ls -lsa');
79+
$process->start();
80+
81+
// do other things
82+
83+
$process->wait(function ($type, $buffer) {
84+
if ('err' === $type) {
85+
echo 'ERR > '.$buffer;
86+
} else {
87+
echo 'OUT > '.$buffer;
88+
}
89+
});
5490

5591
If you want to execute some PHP code in isolation, use the ``PhpProcess``
5692
instead::

0 commit comments

Comments
 (0)