|obj: $r ( Apache2::RequestRec object )|
|arg1: $command ( string )||The command to be $exec()ed.|
|opt arg2: \@argv ( ARRAY ref )||A reference to an array of arguments to be passed to the process as the process ARGV.|
In VOID context returns no filehandles (all std streams to the spawned
process are closed).
In SCALAR context returns the output filehandle of the spawned process (the in and err std streams to the spawned process are closed).
In LIST context returns the input, outpur and error filehandles of the spawned process.
$r->subprocess_env->set($key => $value);
before spawning the subprocess.
There is an issue with reading from the read filehandle ($in_fh)):
A pipe filehandle returned under perlio-disabled Perl needs to call select() if the other end is not fast enough to send the data, since the read is non-blocking.
A pipe filehandle returned under perlio-enabled Perl on the other hand does the select() internally, because its really a filehandle opened via :APR layer, which internally uses APR to communicate with the pipe. The way APR is implemented Perls select() cannot be used with it (mainly because select() wants fileno() and APR is a crossplatform implementation which hides the internal datastructure).
Therefore to write a portable code, you want to use select for perlio-disabled Perl and do nothing for perlio-enabled Perl, hence you can use something similar to the read_data() wrapper shown in the Synopsis section.
Several examples appear in the Synopsis section.
spawn_proc_prog() is similar to fork(), but provides you a better framework to communicate with that process and handles the cleanups for you. But that means that just like fork() it gives you a different process, so you dont use the current Perl interpreter in that new process. If you try to use that method or fork to run a high-performance parallel processing you should look elsewhere. You could try Perl threads, but they are <B>veryB> expensive to start if you have a lot of things loaded into memory (since perl_clone() dups almost everything in the perl land, but the opcode tree). In the mod_perl paradigm this is much more expensive than fork, since normally most of the time we have lots of perl things loaded into memory. Most likely the best solution here is to offload the job to PPerl or some other daemon, with the only added complexity of communication.
To spawn a completely independent process, which will be able to run after Apache has been shutdown and which wont prevent Apache from restarting (releasing the ports Apache is listening to) call spawn_proc_prog() in a void context and make the script detach and close/reopen its communication streams. For example, spawn a process as:
use Apache2::SubProcess ();
$r->spawn_proc_prog (/path/to/detach_script.pl, $args);
and the /path/to/detach_script.pl contents are:
use POSIX setsid;
chdir / or die "Cant chdir to /: $!";
open STDIN, /dev/null or die "Cant read /dev/null: $!";
open STDOUT, +>>, /path/to/apache/error_log
or die "Cant write to /dev/null: $!";
open STDERR, >&STDOUT or die "Cant dup stdout: $!";
setsid or die "Cant start a new session: $!";
# run your code here or call exec to another program
reopening (or closing) the STD streams and called setsid() makes sure that the process is now fully detached from Apache and has a life of its own. chdir() ensures that no partition is tied, in case you need to remount it.
mod_perl 2.0 documentation.
mod_perl 2.0 and its core modules are copyrighted under The Apache Software License, Version 2.0.
The mod_perl development team and numerous contributors.
|perl v5.20.3||WORK::MOD_PERL-2.0.9::DOCS::API::APACHE2::SUBPROCESS (3)||2015-06-18|