Skip to content

Commit 6364989

Browse files
committed
[Process] Add documentation about signal and getPid methods
1 parent 38bbca1 commit 6364989

File tree

1 file changed

+88
-7
lines changed

1 file changed

+88
-7
lines changed

components/process.rst

Lines changed: 88 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -60,34 +60,34 @@ anonymous function to the
6060
echo 'OUT > '.$buffer;
6161
}
6262
});
63-
63+
6464
.. versionadded:: 2.1
6565
The non-blocking feature was added in 2.1.
66-
66+
6767
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
6969
:method:`Symfony\\Component\\Process\\Process::start` method to start an asynchronous
7070
process, the :method:`Symfony\\Component\\Process\\Process::isRunning` method
7171
to check if the process is done and the
7272
:method:`Symfony\\Component\\Process\\Process::getOutput` method to get the output::
7373

7474
$process = new Process('ls -lsa');
7575
$process->start();
76-
76+
7777
while ($process->isRunning()) {
7878
// waiting for process to finish
7979
}
8080

8181
echo $process->getOutput();
82-
82+
8383
You can also wait for a process to end if you started it asynchronously and
8484
are done doing other stuff::
8585

8686
$process = new Process('ls -lsa');
8787
$process->start();
88-
88+
8989
// do other things
90-
90+
9191
$process->wait(function ($type, $buffer) {
9292
if ('err' === $type) {
9393
echo 'ERR > '.$buffer;
@@ -96,6 +96,30 @@ are done doing other stuff::
9696
}
9797
});
9898

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+
99123
If you want to execute some PHP code in isolation, use the ``PhpProcess``
100124
instead::
101125

@@ -148,4 +172,61 @@ check regularly::
148172
usleep(200000);
149173
}
150174

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
151232
.. _Packagist: https://packagist.org/packages/symfony/process

0 commit comments

Comments
 (0)