Alien-uv
view release on metacpan or search on metacpan
libuv/docs/src/guide/processes.rst view on Meta::CPAN
Processes
=========
libuv offers considerable child process management, abstracting the platform
differences and allowing communication with the child process using streams or
named pipes.
A common idiom in Unix is for every process to do one thing and do it well. In
such a case, a process often uses multiple child processes to achieve tasks
(similar to using pipes in shells). A multi-process model with messages
may also be easier to reason about compared to one with threads and shared
memory.
A common refrain against event-based programs is that they cannot take
advantage of multiple cores in modern computers. In a multi-threaded program
the kernel can perform scheduling and assign different threads to different
cores, improving performance. But an event loop has only one thread. The
workaround can be to launch multiple processes instead, with each process
running an event loop, and each process getting assigned to a separate CPU
core.
Spawning child processes
------------------------
The simplest case is when you simply want to launch a process and know when it
exits. This is achieved using ``uv_spawn``.
.. rubric:: spawn/main.c
.. literalinclude:: ../../code/spawn/main.c
:linenos:
:lines: 6-8,15-
:emphasize-lines: 11,13-17
.. NOTE::
``options`` is implicitly initialized with zeros since it is a global
variable. If you change ``options`` to a local variable, remember to
initialize it to null out all unused fields::
uv_process_options_t options = {0};
The ``uv_process_t`` struct only acts as the handle, all options are set via
``uv_process_options_t``. To simply launch a process, you need to set only the
``file`` and ``args`` fields. ``file`` is the program to execute. Since
``uv_spawn`` uses execvp_ internally, there is no need to supply the full
path. Finally as per underlying conventions, **the arguments array has to be
one larger than the number of arguments, with the last element being NULL**.
.. _execvp: http://www.kernel.org/doc/man-pages/online/pages/man3/exec.3.html
After the call to ``uv_spawn``, ``uv_process_t.pid`` will contain the process
ID of the child process.
The exit callback will be invoked with the *exit status* and the type of *signal*
which caused the exit.
.. rubric:: spawn/main.c
.. literalinclude:: ../../code/spawn/main.c
:linenos:
:lines: 9-12
:emphasize-lines: 3
It is **required** to close the process watcher after the process exits.
Changing process parameters
---------------------------
Before the child process is launched you can control the execution environment
using fields in ``uv_process_options_t``.
Change execution directory
++++++++++++++++++++++++++
Set ``uv_process_options_t.cwd`` to the corresponding directory.
Set environment variables
+++++++++++++++++++++++++
``uv_process_options_t.env`` is a null-terminated array of strings, each of the
form ``VAR=VALUE`` used to set up the environment variables for the process. Set
this to ``NULL`` to inherit the environment from the parent (this) process.
Option flags
++++++++++++
Setting ``uv_process_options_t.flags`` to a bitwise OR of the following flags,
modifies the child process behaviour:
* ``UV_PROCESS_SETUID`` - sets the child's execution user ID to ``uv_process_options_t.uid``.
* ``UV_PROCESS_SETGID`` - sets the child's execution group ID to ``uv_process_options_t.gid``.
Changing the UID/GID is only supported on Unix, ``uv_spawn`` will fail on
Windows with ``UV_ENOTSUP``.
* ``UV_PROCESS_WINDOWS_VERBATIM_ARGUMENTS`` - No quoting or escaping of
``uv_process_options_t.args`` is done on Windows. Ignored on Unix.
( run in 0.933 second using v1.01-cache-2.11-cpan-6b5c3043376 )