.\" Automatically generated by Pod::Man 4.11 (Pod::Simple 3.35) .\" .\" Standard preamble: .\" ======================================================================== .de Sp \" Vertical space (when we can't use .PP) .if t .sp .5v .if n .sp .. .de Vb \" Begin verbatim text .ft CW .nf .ne \\$1 .. .de Ve \" End verbatim text .ft R .fi .. .\" Set up some character translations and predefined strings. \*(-- will .\" give an unbreakable dash, \*(PI will give pi, \*(L" will give a left .\" double quote, and \*(R" will give a right double quote. \*(C+ will .\" give a nicer C++. Capital omega is used to do unbreakable dashes and .\" therefore won't be available. \*(C` and \*(C' expand to `' in nroff, .\" nothing in troff, for use with C<>. .tr \(*W- .ds C+ C\v'-.1v'\h'-1p'\s-2+\h'-1p'+\s0\v'.1v'\h'-1p' .ie n \{\ . ds -- \(*W- . ds PI pi . if (\n(.H=4u)&(1m=24u) .ds -- \(*W\h'-12u'\(*W\h'-12u'-\" diablo 10 pitch . if (\n(.H=4u)&(1m=20u) .ds -- \(*W\h'-12u'\(*W\h'-8u'-\" diablo 12 pitch . ds L" "" . ds R" "" . ds C` "" . ds C' "" 'br\} .el\{\ . ds -- \|\(em\| . ds PI \(*p . ds L" `` . ds R" '' . ds C` . ds C' 'br\} .\" .\" Escape single quotes in literal strings from groff's Unicode transform. .ie \n(.g .ds Aq \(aq .el .ds Aq ' .\" .\" If the F register is >0, we'll generate index entries on stderr for .\" titles (.TH), headers (.SH), subsections (.SS), items (.Ip), and index .\" entries marked with X<> in POD. Of course, you'll have to process the .\" output yourself in some meaningful fashion. .\" .\" Avoid warning from groff about undefined register 'F'. .de IX .. .nr rF 0 .if \n(.g .if rF .nr rF 1 .if (\n(rF:(\n(.g==0)) \{\ . if \nF \{\ . de IX . tm Index:\\$1\t\\n%\t"\\$2" .. . if !\nF==2 \{\ . nr % 0 . nr F 2 . \} . \} .\} .rr rF .\" .\" Accent mark definitions (@(#)ms.acc 1.5 88/02/08 SMI; from UCB 4.2). .\" Fear. Run. Save yourself. No user-serviceable parts. . \" fudge factors for nroff and troff .if n \{\ . ds #H 0 . ds #V .8m . ds #F .3m . ds #[ \f1 . ds #] \fP .\} .if t \{\ . ds #H ((1u-(\\\\n(.fu%2u))*.13m) . ds #V .6m . ds #F 0 . ds #[ \& . ds #] \& .\} . \" simple accents for nroff and troff .if n \{\ . ds ' \& . ds ` \& . ds ^ \& . ds , \& . ds ~ ~ . ds / .\} .if t \{\ . ds ' \\k:\h'-(\\n(.wu*8/10-\*(#H)'\'\h"|\\n:u" . ds ` \\k:\h'-(\\n(.wu*8/10-\*(#H)'\`\h'|\\n:u' . ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'^\h'|\\n:u' . ds , \\k:\h'-(\\n(.wu*8/10)',\h'|\\n:u' . ds ~ \\k:\h'-(\\n(.wu-\*(#H-.1m)'~\h'|\\n:u' . ds / \\k:\h'-(\\n(.wu*8/10-\*(#H)'\z\(sl\h'|\\n:u' .\} . \" troff and (daisy-wheel) nroff accents .ds : \\k:\h'-(\\n(.wu*8/10-\*(#H+.1m+\*(#F)'\v'-\*(#V'\z.\h'.2m+\*(#F'.\h'|\\n:u'\v'\*(#V' .ds 8 \h'\*(#H'\(*b\h'-\*(#H' .ds o \\k:\h'-(\\n(.wu+\w'\(de'u-\*(#H)/2u'\v'-.3n'\*(#[\z\(de\v'.3n'\h'|\\n:u'\*(#] .ds d- \h'\*(#H'\(pd\h'-\w'~'u'\v'-.25m'\f2\(hy\fP\v'.25m'\h'-\*(#H' .ds D- D\\k:\h'-\w'D'u'\v'-.11m'\z\(hy\v'.11m'\h'|\\n:u' .ds th \*(#[\v'.3m'\s+1I\s-1\v'-.3m'\h'-(\w'I'u*2/3)'\s-1o\s+1\*(#] .ds Th \*(#[\s+2I\s-2\h'-\w'I'u*3/5'\v'-.3m'o\v'.3m'\*(#] .ds ae a\h'-(\w'a'u*4/10)'e .ds Ae A\h'-(\w'A'u*4/10)'E . \" corrections for vroff .if v .ds ~ \\k:\h'-(\\n(.wu*9/10-\*(#H)'\s-2\u~\d\s+2\h'|\\n:u' .if v .ds ^ \\k:\h'-(\\n(.wu*10/11-\*(#H)'\v'-.4m'^\v'.4m'\h'|\\n:u' . \" for low resolution devices (crt and lpr) .if \n(.H>23 .if \n(.V>19 \ \{\ . ds : e . ds 8 ss . ds o a . ds d- d\h'-1'\(ga . ds D- D\h'-1'\(hy . ds th \o'bp' . ds Th \o'LP' . ds ae ae . ds Ae AE .\} .rm #[ #] #H #V #F C .\" ======================================================================== .\" .IX Title "Mojo::IOLoop::ReadWriteProcess 3pm" .TH Mojo::IOLoop::ReadWriteProcess 3pm "2020-10-02" "perl v5.30.3" "User Contributed Perl Documentation" .\" For nroff, turn off justification. Always turn off hyphenation; it makes .\" way too many mistakes in technical documents. .if n .ad l .nh .SH "NAME" Mojo::IOLoop::ReadWriteProcess \- Execute external programs or internal code blocks as separate process. .SH "SYNOPSIS" .IX Header "SYNOPSIS" .Vb 1 \& use Mojo::IOLoop::ReadWriteProcess; \& \& # Code fork \& my $process = Mojo::IOLoop::ReadWriteProcess\->new(sub { print "Hello\en" }); \& $process\->start(); \& print "Running\en" if $process\->is_running(); \& $process\->getline(); # Will return "Hello\en" \& $process\->pid(); # Process id \& $process\->stop(); \& $process\->wait_stop(); # if you intend to wait its lifespan \& \& # Methods can be chained, thus this is valid: \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $output = process( sub { print "Hello\en" } )\->start()\->wait_stop\->getline; \& \& # Handles seamelessy also external processes: \& my $process = process(execute=> \*(Aq/path/to/bin\*(Aq )\->args(qw(foo bar baz)); \& $process\->start(); \& my $line_output = $process\->getline(); \& my $pid = $process\->pid(); \& $process\->stop(); \& my @errors = $process\->error; \& \& # Get process return value \& $process = process( sub { return "256"; } )\->start()\->wait_stop; \& # We need to stop it to retrieve the exit status \& my $return = $process\->return_status; \& \& # We can access directly to handlers from the object: \& my $stdout = $process\->read_stream; \& my $stdin = $process\->write_stream; \& my $stderr = $process\->error_stream; \& \& # So this works: \& print $stdin "foo bar\en"; \& my @lines = <$stdout>; \& \& # There is also an alternative channel of communication (just for forked processes): \& my $channel_in = $process\->channel_in; # write to the child process \& my $channel_out = $process\->channel_out; # read from the child process \& $process\->channel_write("PING"); # convenience function .Ve .SH "DESCRIPTION" .IX Header "DESCRIPTION" Mojo::IOLoop::ReadWriteProcess is yet another process manager. .SH "EVENTS" .IX Header "EVENTS" Mojo::IOLoop::ReadWriteProcess inherits all events from Mojo::EventEmitter and can emit the following new ones. .SS "start" .IX Subsection "start" .Vb 4 \& $process\->on(start => sub { \& my ($process) = @_; \& $process\->is_running(); \& }); .Ve .PP Emitted when the process starts. .SS "stop" .IX Subsection "stop" .Vb 4 \& $process\->on(stop => sub { \& my ($process) = @_; \& $process\->restart(); \& }); .Ve .PP Emitted when the process stops. .SS "process_error" .IX Subsection "process_error" .Vb 4 \& $process\->on(process_error => sub { \& my ($e) = @_; \& my @errors = @{$e}; \& }); .Ve .PP Emitted when the process produce errors. .SS "process_stuck" .IX Subsection "process_stuck" .Vb 4 \& $process\->on(process_stuck => sub { \& my ($self) = @_; \& ... \& }); .Ve .PP Emitted when \f(CW\*(C`blocking_stop\*(C'\fR is set and all attempts for killing the process in \f(CW\*(C`max_kill_attempts\*(C'\fR have been exhausted. The event is emitted before attempting to kill it with \s-1SIGKILL\s0 and becoming blocking. .SS "\s-1SIG_CHLD\s0" .IX Subsection "SIG_CHLD" .Vb 4 \& $process\->on(SIG_CHLD => sub { \& my ($self) = @_; \& ... \& }); .Ve .PP Emitted when we receive \s-1SIG_CHLD.\s0 .SS "\s-1SIG_TERM\s0" .IX Subsection "SIG_TERM" .Vb 4 \& $process\->on(SIG_TERM => sub { \& my ($self) = @_; \& ... \& }); .Ve .PP Emitted when the child forked process receives \s-1SIG_TERM,\s0 before exiting. .SS "collected" .IX Subsection "collected" .Vb 4 \& $process\->on(collected => sub { \& my ($self) = @_; \& ... \& }); .Ve .PP Emitted right after status collection. .SS "collect_status" .IX Subsection "collect_status" .Vb 4 \& $process\->on(collect_status => sub { \& my ($self) = @_; \& ... \& }); .Ve .PP Emitted when on child process waitpid. It is used internally to get the child process status. Note: events attached to it are wiped when process has been stopped. .SH "ATTRIBUTES" .IX Header "ATTRIBUTES" Mojo::IOLoop::ReadWriteProcess inherits all attributes from Mojo::EventEmitter and implements the following new ones. .SS "execute" .IX Subsection "execute" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess; \& my $process = Mojo::IOLoop::ReadWriteProcess\->new(execute => "/usr/bin/perl"); \& $process\->start(); \& $process\->on( stop => sub { print "Process: ".(+shift()\->pid)." finished"; } ); \& $process\->stop(); .Ve .PP \&\f(CW\*(C`execute\*(C'\fR should contain the external program that you wish to run. .SS "code" .IX Subsection "code" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess; \& my $process = Mojo::IOLoop::ReadWriteProcess\->new(code => sub { print "Hello" } ); \& $process\->start(); \& $process\->on( stop => sub { print "Process: ".(+shift()\->pid)." finished"; } ); \& $process\->stop(); .Ve .PP It represent the code you want to run in background. .PP You do not need to specify \f(CW\*(C`code\*(C'\fR, it is implied if no arguments is given. .PP .Vb 4 \& my $process = Mojo::IOLoop::ReadWriteProcess\->new(sub { print "Hello" }); \& $process\->start(); \& $process\->on( stop => sub { print "Process: ".(+shift()\->pid)." finished"; } ); \& $process\->stop(); .Ve .SS "args" .IX Subsection "args" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess; \& my $process = Mojo::IOLoop::ReadWriteProcess\->new(code => sub { print "Hello ".$_[1] }, args => "User" ); \& $process\->start(); \& $process\->on( stop => sub { print "Process: ".(+shift()\->pid)." finished"; } ); \& $process\->stop(); \& \& # The process will print "Hello User" .Ve .PP Array or arrayref of options to pass by to the external binary or the code block. .SS "blocking_stop" .IX Subsection "blocking_stop" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess; \& my $process = Mojo::IOLoop::ReadWriteProcess\->new(code => sub { print "Hello" }, blocking_stop => 1 ); \& $process\->start(); \& $process\->on( stop => sub { print "Process: ".(+shift()\->pid)." finished"; } ); \& $process\->stop(); # Will wait indefinitely until the process is stopped .Ve .PP Set it to 1 if you want to do blocking stop of the process. .SS "channels" .IX Subsection "channels" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess; \& my $process = Mojo::IOLoop::ReadWriteProcess\->new(code => sub { print "Hello" }, channels => 0 ); \& $process\->start(); \& $process\->on( stop => sub { print "Process: ".(+shift()\->pid)." finished"; } ); \& $process\->stop(); # Will wait indefinitely until the process is stopped .Ve .PP Set it to 0 if you want to disable internal channels. .SS "session" .IX Subsection "session" .Vb 4 \& use Mojo::IOLoop::ReadWriteProcess; \& my $process = Mojo::IOLoop::ReadWriteProcess\->new(sub { print "Hello" }); \& my $session = $process\->session; \& $session\->enable_subreaper; .Ve .PP Returns the current Mojo::IOLoop::ReadWriteProcess::Session singleton. .SS "subreaper" .IX Subsection "subreaper" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess; \& my $process = Mojo::IOLoop::ReadWriteProcess\->new(code => sub { print "Hello ".$_[1] }, args => "User" ); \& $process\->subreaper(1)\->start(); \& $process\->on( stop => sub { shift()\->disable_subreaper } ); \& $process\->stop(); \& \& # The process will print "Hello User" .Ve .PP Mark the current process (not the child) as subreaper on start. It's on invoker behalf to disable subreaper when process stops, as it marks the current process and not the child. .SS "ioloop" .IX Subsection "ioloop" .Vb 2 \& my $loop = $process\->ioloop; \& $subprocess = $process\->ioloop(Mojo::IOLoop\->new); .Ve .PP Event loop object to control, defaults to the global Mojo::IOLoop singleton. .SS "max_kill_attempts" .IX Subsection "max_kill_attempts" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess; \& my $process = Mojo::IOLoop::ReadWriteProcess\->new(code => sub { print "Hello" }, max_kill_attempts => 50 ); \& $process\->start(); \& $process\->on( stop => sub { print "Process: ".(+shift()\->pid)." finished"; } ); \& $process\->stop(); # It will attempt to send SIGTERM 50 times. .Ve .PP Defaults to \f(CW5\fR, is the number of attempts before bailing out. .PP It can be used with blocking_stop, so if the number of attempts are exhausted, a \s-1SIGKILL\s0 and waitpid will be tried at the end. .SS "kill_whole_group" .IX Subsection "kill_whole_group" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess; \& my $process = Mojo::IOLoop::ReadWriteProcess\->new(code => sub { setpgrp(0, 0); exec(...); }, kill_whole_group => 1 ); \& $process\->start(); \& $process\->send_signal(...); # Will skip the usual check whether $process\->pid is running \& $process\->stop(); # Kills the entire process group and waits for all processes in the group to finish .Ve .PP Defaults to \f(CW0\fR, whether to send signals (e.g. to stop) to the entire process group. .PP This is useful when the sub process creates further sub processes and creates a new process group as shown in the example. In this case it might be useful to take care of the entire process group when stopping and wait for every process in the group to finish. .SS "collect_status" .IX Subsection "collect_status" Defaults to \f(CW1\fR, If enabled it will automatically collect the status of the children process. Disable it in case you want to manage your process child directly, and do not want to rely on automatic collect status. If you won't overwrite your \f(CW\*(C`SIGCHLD\*(C'\fR handler, the \f(CW\*(C`SIG_CHLD\*(C'\fR event will be still emitted. .SS "serialize" .IX Subsection "serialize" Defaults to \f(CW0\fR, If enabled data returned from forked process will be serialized with Storable. .SS "kill_sleeptime" .IX Subsection "kill_sleeptime" Defaults to \f(CW1\fR, it's the seconds to wait before attempting \s-1SIGKILL\s0 when blocking_stop is setted to 1. .SS "separate_err" .IX Subsection "separate_err" Defaults to \f(CW1\fR, it will create a separate channel to intercept process \s-1STDERR,\s0 otherwise it will be redirected to \s-1STDOUT.\s0 .SS "verbose" .IX Subsection "verbose" Defaults to \f(CW1\fR, it indicates message verbosity. .SS "set_pipes" .IX Subsection "set_pipes" Defaults to \f(CW1\fR, If enabled, additional pipes for process communication are automatically set up. .SS "internal_pipes" .IX Subsection "internal_pipes" Defaults to \f(CW1\fR, If enabled, additional pipes for retreiving process return and errors are set up. Note: If you disable that, the only information provided by the process will be the exit_status. .SS "autoflush" .IX Subsection "autoflush" Defaults to \f(CW1\fR, If enabled autoflush of handlers is enabled automatically. .SS "error" .IX Subsection "error" Returns a Mojo::Collection of errors. Note: errors that can be captured only at the end of the process .SH "METHODS" .IX Header "METHODS" Mojo::IOLoop::ReadWriteProcess inherits all methods from Mojo::EventEmitter and implements the following new ones. .SS "\fBstart()\fP" .IX Subsection "start()" .Vb 4 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(sub { \& print STDERR "Boo\en" \& } )\->start; .Ve .PP Starts the process .SS "\fBstop()\fP" .IX Subsection "stop()" .Vb 2 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process( execute => "/path/to/bin" )\->start\->stop; .Ve .PP Stop the process. Unless you use \f(CW\*(C`wait_stop()\*(C'\fR, it will attempt to kill the process without waiting the process to finish. By defaults it send \f(CW\*(C`SIGTERM\*(C'\fR to the child. You can change that by defining the internal attribute \f(CW\*(C`_default_kill_signal\*(C'\fR. Note, if you want to be *sure* that the process gets killed, you can enable the \&\f(CW\*(C`blocking_stop\*(C'\fR attribute, that will attempt to send \f(CW\*(C`SIGKILL\*(C'\fR after \f(CW\*(C`max_kill_attempts\*(C'\fR is reached. .SS "\fBrestart()\fP" .IX Subsection "restart()" .Vb 2 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process( execute => "/path/to/bin" )\->restart; .Ve .PP It restarts the process if stopped, or if already running, it stops it first. .SS "\fBis_running()\fP" .IX Subsection "is_running()" .Vb 3 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process( execute => "/path/to/bin" )\->start; \& $p\->is_running; .Ve .PP Boolean, it inspect if the process is currently running or not. .SS "\fBexit_status()\fP" .IX Subsection "exit_status()" .Vb 2 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process( execute => "/path/to/bin" )\->start; \& \& $p\->wait_stop\->exit_status; .Ve .PP Inspect the process exit status, it does the shifting magic, to access to the real value call \f(CW\*(C`_status()\*(C'\fR. .SS "\fBreturn_status()\fP" .IX Subsection "return_status()" .Vb 2 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process( sub { return 42 } )\->start; \& \& my $s = $p\->wait_stop\->return_status; # 42 .Ve .PP Inspect the codeblock return. .SS "\fBenable_subreaper()\fP" .IX Subsection "enable_subreaper()" .Vb 2 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process()\->enable_subreaper; .Ve .PP Mark the current process (not the child) as subreaper. This is used typically if you want to mark further childs as subreapers inside other forks. .PP .Vb 4 \& my $master_p = process( \& sub { \& my $p = shift; \& $p\->enable_subreaper; \& \& process(sub { sleep 4; exit 1 })\->start(); \& process( \& sub { \& sleep 4; \& process(sub { sleep 1; })\->start(); \& })\->start(); \& process(sub { sleep 4; exit 0 })\->start(); \& process(sub { sleep 4; die })\->start(); \& my $manager \& = process(sub { sleep 2 })\->subreaper(1)\->start(); \& sleep 1 for (0 .. 10); \& $manager\->stop; \& return $manager\->session\->all\->size; \& }); \& \& $master_p\->subreaper(1); \& \& $master_p\->on(collected => sub { $status++ }); \& \& # On start we setup the current process as subreaper \& # So it\*(Aqs up on us to disable it after process is done. \& $master_p\->on(stop => sub { shift()\->disable_subreaper }); \& $master_p\->start(); .Ve .SS "\fBdisable_subreaper()\fP" .IX Subsection "disable_subreaper()" .Vb 2 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process()\->disable_subreaper; .Ve .PP Unset the current process (not the child) as subreaper. .SS "\fBprctl()\fP" .IX Subsection "prctl()" .Vb 3 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(); \& $p\->prctl($option, $arg2, $arg3, $arg4, $arg5); .Ve .PP Internal function to execute and wrap the prctl syscall, accepts the same arguments as prctl. .SS "\fBdiag()\fP" .IX Subsection "diag()" .Vb 4 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(sub { print "Hello\en" }); \& $p\->on( stop => sub { shift\->diag("Done!") } ); \& $p\->start\->wait_stop; .Ve .PP Internal function to print information to \s-1STDERR\s0 if verbose attribute is set or either \s-1DEBUG\s0 mode enabled. You can use it if you wish to display information on the process status. .SS "\fBto_ioloop()\fP" .IX Subsection "to_ioloop()" .Vb 1 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& \& my $p = process(sub { print "Hello from first process\en"; sleep 1 }); \& \& $p\->start(); # Start and sets the handlers \& my $stream = $p\->to_ioloop; # Get the stream and demand to IOLoop \& my $output; \& \& # Hook on Mojo::IOLoop::Stream events \& $stream\->on(read => sub { $output .= pop; $p\->is_running ... }); \& \& Mojo::IOLoop\->singleton\->start() unless Mojo::IOLoop\->singleton\->is_running; .Ve .PP Returns a Mojo::IOLoop::Stream object and demand the wait operation to Mojo::IOLoop. It needs \f(CW\*(C`set_pipes\*(C'\fR enabled. Default IOLoop can be overridden in \f(CW\*(C`ioloop()\*(C'\fR. .SS "\fBwait()\fP" .IX Subsection "wait()" .Vb 3 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(sub { print "Hello\en" })\->wait; \& # ... here now you can mangle $p handlers and such .Ve .PP Waits until the process finishes, but does not performs cleanup operations (until stop is called). .SS "\fBwait_stop()\fP" .IX Subsection "wait_stop()" .Vb 3 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(sub { print "Hello\en" })\->start\->wait_stop; \& # $p is not running anymore, and all possible events have been granted to be emitted. .Ve .PP Waits until the process finishes, and perform cleanup operations. .SS "\fBerrored()\fP" .IX Subsection "errored()" .Vb 3 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(sub { die "Nooo" })\->start\->wait_stop; \& $p\->errored; # will return "1" .Ve .PP Returns a boolean indicating if the process had errors or not. .SS "\fBwrite_pidfile()\fP" .IX Subsection "write_pidfile()" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(sub { die "Nooo" } ); \& $p\->pidfile("foobar"); \& $p\->start(); \& $p\->write_pidfile(); .Ve .PP Forces writing \s-1PID\s0 of process to specified pidfile in the attributes of the object. Useful only if the process have been already started, otherwise if a pidfile it's supplied as attribute, it will be done automatically. .SS "\fBwrite_stdin()\fP" .IX Subsection "write_stdin()" .Vb 4 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(sub { my $a = ; print STDERR "Hello my name is $a\en"; } )\->start; \& $p\->write_stdin("Larry"); \& $p\->read_stderr; # process STDERR will contain: "Hello my name is Larry\en" .Ve .PP Write data to process \s-1STDIN.\s0 .SS "\fBwrite_channel()\fP" .IX Subsection "write_channel()" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(sub { \& my $self = shift; \& my $parent_output = $self\->channel_out; \& my $parent_input = $self\->channel_in; \& \& while(defined(my $line = <$parent_input>)) { \& print $parent_output "PONG\en" if $line =~ /PING/i; \& } \& } )\->start; \& $p\->write_channel("PING"); \& my $out = $p\->read_channel; \& # $out is PONG \& my $child_output = $p\->channel_out; \& while(defined(my $line = <$child_output>)) { \& print "Process is replying back with $line!\en"; \& $p\->write_channel("PING"); \& } .Ve .PP Write data to process channel. Note, it's not \s-1STDIN,\s0 neither \s-1STDOUT,\s0 it's a complete separate channel dedicated to parent-child communication. In the parent process, you can access to the same pipes (but from the opposite direction): .PP .Vb 2 \& my $child_output = $self\->channel_out; \& my $child_input = $self\->channel_in; .Ve .SS "\fBread_stdout()\fP" .IX Subsection "read_stdout()" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(sub { \& print "Boo\en" \& } )\->start; \& $p\->read_stdout; .Ve .PP Gets a single line from process \s-1STDOUT.\s0 .SS "\fBread_channel()\fP" .IX Subsection "read_channel()" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(sub { \& my $self = shift; \& my $parent_output = $self\->channel_out; \& my $parent_input = $self\->channel_in; \& \& print $parent_output "PONG\en"; \& } )\->start; \& $p\->read_channel; .Ve .PP Gets a single line from process channel. .SS "\fBread_stderr()\fP" .IX Subsection "read_stderr()" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(sub { \& print STDERR "Boo\en" \& } )\->start; \& $p\->read_stderr; .Ve .PP Gets a single line from process \s-1STDERR.\s0 .SS "\fBread_all_stdout()\fP" .IX Subsection "read_all_stdout()" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(sub { \& print "Boo\en" \& } )\->start; \& $p\->read_all_stdout; .Ve .PP Gets all the \s-1STDOUT\s0 output of the process. .SS "\fBread_all_channel()\fP" .IX Subsection "read_all_channel()" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(sub { \& shift\->channel_out\->write("Ping") \& } )\->start; \& $p\->read_all_channel; .Ve .PP Gets all the channel output of the process. .SS "\fBread_all_stderr()\fP" .IX Subsection "read_all_stderr()" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process(sub { \& print STDERR "Boo\en" \& } )\->start; \& $p\->read_all_stderr; .Ve .PP Gets all the \s-1STDERR\s0 output of the process. .SS "\fBsend_signal()\fP" .IX Subsection "send_signal()" .Vb 3 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& use POSIX; \& my $p = process( execute => "/path/to/bin" )\->start; \& \& $p\->send_signal(POSIX::SIGKILL); .Ve .PP Send a signal to the process .SH "EXPORTS" .IX Header "EXPORTS" .SS "\fBparallel()\fP" .IX Subsection "parallel()" .Vb 5 \& use Mojo::IOLoop::ReadWriteProcess qw(parallel); \& my $pool = parallel sub { print "Hello\en" } => 5; \& $pool\->start(); \& $pool\->on( stop => sub { print "Process: ".(+shift()\->pid)." finished"; } ); \& $pool\->stop(); .Ve .PP Returns a Mojo::IOLoop::ReadWriteProcess::Pool object that represent a group of processes. .PP It accepts the same arguments as Mojo::IOLoop::ReadWriteProcess, and the last one represent the number of processes to generate. .SS "\fBbatch()\fP" .IX Subsection "batch()" .Vb 4 \& use Mojo::IOLoop::ReadWriteProcess qw(batch); \& my $pool = batch; \& $pool\->add(sub { print "Hello\en" }); \& $pool\->on(stop => sub { shift\->_diag("Done!") })\->start\->wait_stop; .Ve .PP Returns a Mojo::IOLoop::ReadWriteProcess::Pool object generated from supplied arguments. It accepts as input the same parameter of Mojo::IOLoop::ReadWriteProcess::Pool constructor ( see \fBparallel()\fR ). .SS "\fBprocess()\fP" .IX Subsection "process()" .Vb 3 \& use Mojo::IOLoop::ReadWriteProcess qw(process); \& my $p = process sub { print "Hello\en" }; \& $p\->start()\->wait_stop; .Ve .PP or even: .PP .Vb 1 \& process(sub { print "Hello\en" })\->start\->wait_stop; .Ve .PP Returns a Mojo::IOLoop::ReadWriteProcess object that represent a process. .PP It accepts the same arguments as Mojo::IOLoop::ReadWriteProcess. .SS "\fBqueue()\fP" .IX Subsection "queue()" .Vb 4 \& use Mojo::IOLoop::ReadWriteProcess qw(queue); \& my $q = queue; \& $q\->add(sub { return 42 } ); \& $q\->consume; .Ve .PP Returns a Mojo::IOLoop::ReadWriteProcess::Queue object that represent a queue. .SH "DEBUGGING" .IX Header "DEBUGGING" You can set the \s-1MOJO_EVENTEMITTER_DEBUG\s0 environment variable to get some advanced diagnostics information printed to \s-1STDERR.\s0 .PP .Vb 1 \& MOJO_EVENTEMITTER_DEBUG=1 .Ve .PP Also, you can set \s-1MOJO_PROCESS_DEBUG\s0 environment variable to get diagnostics about the process execution. .PP .Vb 1 \& MOJO_PROCESS_DEBUG=1 .Ve .SH "LICENSE" .IX Header "LICENSE" Copyright (C) Ettore Di Giacinto. .PP This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself. .SH "AUTHOR" .IX Header "AUTHOR" Ettore Di Giacinto