@@ -26,15 +26,16 @@ a command in a sub-process::
26
26
$process = new Process('ls -lsa');
27
27
$process->setTimeout(3600);
28
28
$process->run();
29
+
30
+ // executes after the the command finishes
29
31
if (!$process->isSuccessful()) {
30
32
throw new \RuntimeException($process->getErrorOutput());
31
33
}
32
34
33
35
print $process->getOutput();
34
36
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.
38
39
39
40
When executing a long running command (like rsync-ing files to a remote
40
41
server), you can give feedback to the end user in real-time by passing an
@@ -51,6 +52,41 @@ anonymous function to the
51
52
echo 'OUT > '.$buffer;
52
53
}
53
54
});
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
+ });
54
90
55
91
If you want to execute some PHP code in isolation, use the ``PhpProcess ``
56
92
instead::
0 commit comments