NAME
IO::Async::ChildManager
- facilitates the execution of child processes
SYNOPSIS
This object is used indirectly via an IO::Async::Loop
:
use IO::Async::Loop;
my $loop = IO::Async::Loop->new();
...
$loop->watch_child( 1234 => sub { print "Child 1234 exited\n" } );
$loop->spawn_child(
command => "/usr/bin/something",
on_exit => \&exit_handler,
setup => [
stdout => $pipe,
]
);
DESCRIPTION
This module extends the functionallity of the containing IO::Async::Loop
to manage the execution of child processes. It acts as a central point to store PID values of currently-running children, and to call the appropriate callback handler code when the process terminates.
METHODS
When active, the following methods are available on the containing Loop
object.
$loop->watch_child( $kid, $code )
This method adds a new handler for the termination of the given child PID.
- $kid
-
The PID to watch.
- $code
-
A CODE reference to the handling function. It will be invoked as
$code->( $pid, $? )
After invocation, the handler is automatically removed from the manager.
$watching = $manager->is_watching( $kid )
This method tests if the manager is currently watching for termination of the given PID. It returns a boolean value.
@kids = $manager->list_watching()
This method returns a list of the PIDs that the manager is currently watching for. The list is returned in no particular order.
$pid = $loop->detach_child( %params )
This method creates a new child process to run a given code block.
- code => CODE
-
A block of code to execute in the child process. It will be called in scalar context inside an
eval
block. The return value will be used as theexit()
code from the child if it returns (or 255 if it returnedundef
or thows an exception). - on_exit => CODE
-
A optional callback function to be called when the child processes exits. It will be invoked in the following way:
$on_exit->( $pid, $exitcode )
This key is optional; if not supplied, the calling code should install a handler using the
watch_child()
method. - keep_signals => BOOL
-
Optional boolean. If missing or false, any CODE references in the
%SIG
hash will be removed and restored back toDEFAULT
in the child process. If true, no adjustment of the%SIG
hash will be performed.
$pid = $loop->spawn_child( %params )
This method creates a new child process to run a given code block or command. The %params
hash takes the following keys:
- command => ARRAY or STRING
-
Either a reference to an array containing the command and its arguments, or a plain string containing the command. This value is passed into perl's
exec()
function. - code => CODE
-
A block of code to execute in the child process. It will be called in scalar context inside an
eval
block. - setup => ARRAY
-
A reference to an array which gives file descriptors to set up in the child process before running the code or command. See below.
- on_exit => CODE
-
A callback function to be called when the child processes exits. It will be invoked in the following way:
$on_exit->( $pid, $exitcode, $dollarbang, $dollarat )
Exactly one of the command
or code
keys must be specified.
If the command
key is used, the given array or string is executed using the exec()
function.
If the code
key is used, the return value will be used as the exit()
code from the child if it returns (or 255 if it returned undef
or thows an exception).
Case | WEXITSTATUS($exitcode) | $dollarbang | $dollarat
----------------+------------------------+-------------+----------
exec() succeeds | exit code from program | 0 | ""
exec() fails | 255 | $! | ""
$code returns | return value | $! | ""
$code dies | 255 | $! | $@
It is usually more convenient to use the open()
method in simple cases where an external program is being started in order to interact with it via file IO.
setup
array
This array gives a list of file descriptor operations to perform in the child process after it has been fork()
ed from the parent, before running the code or command. It consists of name/value pairs which are ordered; the operations are performed in the order given.
- fdn => ARRAY
-
Gives an operation on file descriptor n. The first element of the array defines the operation to be performed:
- [ 'close' ]
-
The file descriptor will be closed.
- [ 'dup', $io ]
-
The file descriptor will be
dup2()
ed from the given IO handle. - [ 'open', $mode, $file ]
-
The file descriptor will be opened from the named file in the given mode. The
$mode
string should be in the form usually given to theopen()
function; such as '<' or '>>'. - [ 'keep' ]
-
The file descriptor will not be closed; it will be left as-is.
A non-reference value may be passed as a shortcut, where it would contain the name of the operation with no arguments (i.e. for the
close
andkeep
operations). - IO => ARRAY
-
Shortcut for passing
fdn
, where n is the fileno of the IO reference. In this case, the key must be a reference that implements thefileno
method. This is mostly useful for$handle => 'keep'
- fdn => IO
-
A shortcut for the
dup
case given above. - stdin => ...
- stdout => ...
- stderr => ...
-
Shortcuts for
fd0
,fd1
andfd2
respectively. - env => HASH
-
A reference to a hash to set as the child process's environment.
- nice => INT
-
Change the child process's scheduling priority using
POSIX::nice()
. - chdir => STRING
-
Change the child process's working directory using
chdir()
.
If no directions for what to do with stdin
, stdout
and stderr
are given, a default of keep
is implied. All other file descriptors will be closed, unless a keep
operation is given for them.
$pid = $loop->open_child( %params )
This creates a new child process to run the given code block or command, and attaches filehandles to it that the parent will watch. The %params
hash takes the following keys:
- command => ARRAY or STRING
- code => CODE
-
The command or code to run in the child process (as per the
spawn
method) - on_finish => CODE
-
A callback function to be called when the child process exits and has closed all of the filehandles that were set up for it. It will be invoked in the following way:
$on_finish->( $pid, $exitcode )
- on_error => CODE
-
Optional callback to be called when the child code block throws an exception, or the command could not be
exec()
ed. It will be invoked in the following way (as perspawn
)$on_error->( $pid, $exitcode, $dollarbang, $dollarat )
If this callback is not supplied, then
on_finish
is used instead. The value of$!
and$@
will not be reported. - setup => ARRAY
-
Optional reference to an array to pass to the underlying
spawn
method.
In addition, the hash takes keys that define how to set up file descriptors in the child process. (If the setup
array is also given, these operations will be performed after those specified by setup
.)
- fdn => HASH
-
A hash describing how to set up file descriptor n. The hash may contain one of the following sets of keys:
- on_read => CODE
-
The child will be given the writing end of a pipe. The reading end will be wrapped by an
IO::Async::Stream
using thison_read
callback function. - from => STRING
-
The child will be given the reading end of a pipe. The string given by the
from
parameter will be written to the child. When all of the data has been written the pipe will be closed.
- stdin => ...
- stdout => ...
- stderr => ...
-
Shortcuts for
fd0
,fd1
andfd2
respectively.
$pid = $loop->run_child( %params )
This creates a new child process to run the given code block or command, capturing its STDOUT and STDERR streams. When the process exits, the callback is invoked being passed the exitcode, and content of the streams.
- command => ARRAY or STRING
- code => CODE
-
The command or code to run in the child process (as per the
spawn
method) - on_finish => CODE
-
A callback function to be called when the child process exits and closed its STDOUT and STDERR streams. It will be invoked in the following way:
$on_finish->( $pid, $exitcode, $stdout, $stderr )
- stdin => STRING
-
Optional. String to pass in to the child process's STDIN stream.
This function is intended mainly as an IO::Async-compatible replacement for the perl readpipe
function (`backticks`), allowing it to replace
my $output = `command here`;
with
$loop->run_child(
command => "command here",
on_finish => sub {
my ( undef, $exitcode, $output ) = @_;
...
}
);
AUTHOR
Paul Evans <leonerd@leonerd.org.uk>