@@ -60,34 +60,34 @@ anonymous function to the
60
60
echo 'OUT > '.$buffer;
61
61
}
62
62
});
63
-
63
+
64
64
.. versionadded :: 2.1
65
65
The non-blocking feature was added in 2.1.
66
-
66
+
67
67
You can also start the subprocess and then let it run asynchronously, retrieving
68
- output and the status in your main process whenever you need it. Use the
68
+ output and the status in your main process whenever you need it. Use the
69
69
:method: `Symfony\\ Component\\ Process\\ Process::start ` method to start an asynchronous
70
70
process, the :method: `Symfony\\ Component\\ Process\\ Process::isRunning ` method
71
71
to check if the process is done and the
72
72
:method: `Symfony\\ Component\\ Process\\ Process::getOutput ` method to get the output::
73
73
74
74
$process = new Process('ls -lsa');
75
75
$process->start();
76
-
76
+
77
77
while ($process->isRunning()) {
78
78
// waiting for process to finish
79
79
}
80
80
81
81
echo $process->getOutput();
82
-
82
+
83
83
You can also wait for a process to end if you started it asynchronously and
84
84
are done doing other stuff::
85
85
86
86
$process = new Process('ls -lsa');
87
87
$process->start();
88
-
88
+
89
89
// do other things
90
-
90
+
91
91
$process->wait(function ($type, $buffer) {
92
92
if ('err' === $type) {
93
93
echo 'ERR > '.$buffer;
@@ -96,6 +96,30 @@ are done doing other stuff::
96
96
}
97
97
});
98
98
99
+ .. note ::
100
+
101
+ Please consider that the :method: `Symfony\\ Component\\ Process\\ Process::wait `
102
+ method is blocking.
103
+
104
+ .. versionadded :: 2.3
105
+ The ``signal `` parameter of the ``stop `` method was added in Symfony 2.3.
106
+
107
+ Any asynchronous process can be stopped at any time with the
108
+ :method: `Symfony\\ Component\\ Process\\ Process::stop ` method. This method takes
109
+ two arguments : a timeout and a signal. Once the timeout is reached, the signal
110
+ is sent to the running process.
111
+ The default signal sent to a process is ``SIGKILL ``. Please read the signal
112
+ documentation below to know more about signal handling in the Process component.
113
+
114
+ .. code-block :: php
115
+
116
+ $process = new Process('ls -lsa');
117
+ $process->start();
118
+
119
+ // ... do other things
120
+
121
+ $process->stop(3, SIGINT);
122
+
99
123
If you want to execute some PHP code in isolation, use the ``PhpProcess ``
100
124
instead::
101
125
@@ -148,4 +172,61 @@ check regularly::
148
172
usleep(200000);
149
173
}
150
174
175
+ Process Signals
176
+ ---------------
177
+
178
+ .. versionadded :: 2.3
179
+ The ``signal `` method was added in Symfony 2.3.
180
+
181
+ When running programs asynchronously, you can send it posix signals with the
182
+ :method: `Symfony\\ Component\\ Process\\ Process::signal ` method.
183
+
184
+ .. code-block :: php
185
+
186
+ use Symfony\Component\Process\Process;
187
+
188
+ $process = new Process('find / -name "rabbit"');
189
+ $process->start();
190
+
191
+ // will send a SIGKILL to the process
192
+ $process->signal(SIGKILL);
193
+
194
+ .. caution ::
195
+
196
+ Due to some limitations in PHP, if you're using signals with the Process
197
+ component, you may have to prefix your commands with `exec `_. Please read
198
+ `Symfony Issue#5769 `_ and `PHP Bug#39992 `_ to understand why this is happening.
199
+
200
+ POSIX signals are not available on Windows platforms, please refer to the
201
+ `PHP documentation `_ for available signals.
202
+
203
+ Process Pid
204
+ -----------
205
+
206
+ .. versionadded :: 2.3
207
+ The ``getPid `` method was added in Symfony 2.3.
208
+
209
+ You can access the `pid `_ of a running process with the
210
+ :method: `Symfony\\ Component\\ Process\\ Process::getPid ` method.
211
+
212
+ .. code-block :: php
213
+
214
+ use Symfony\Component\Process\Process;
215
+
216
+ $process = new Process('/usr/bin/php worker.php');
217
+ $process->start();
218
+
219
+ $pid = $process->getPid();
220
+
221
+ .. caution ::
222
+
223
+ Due to some limitations in PHP, if you want to get the pid of a symfony Process,
224
+ you may have to prefix your commands with `exec `_. Please read
225
+ `Symfony Issue#5769 `_ to understand why this is happening.
226
+
227
+ .. _Symfony Issue#5759 : https://github.com/symfony/symfony/issues/5759
228
+ .. _PHP Bug#39992 : https://bugs.php.net/bug.php?id=39992
229
+ .. _exec : http://en.wikipedia.org/wiki/Exec_(operating_system)
230
+ .. _pid : http://en.wikipedia.org/wiki/Process_identifier
231
+ .. _PHP Documentation : http://php.net/manual/en/pcntl.constants.php
151
232
.. _Packagist : https://packagist.org/packages/symfony/process
0 commit comments