Hello,
I have a mod_perl2 application that searches some databases (custom
socket connections, no DBI).
Now I would like to run the searches in parallel, collect all the
results and return them after some post-processing.
I searched the net and asked on perlmonks
(http://www.perlmonks.org/?node_id=650263) and came to this not yet
working code (taken mostly from an example in practical mod_perl):
sub metasearch {
my $self = shift;
my $db_ref = $self->db_defs;
my @dbs = @{$self->dbs};
my $logger = $self->logger;
$SIG{CHLD} = 'IGNORE';
my @result;
foreach my $db (@dbs) {
defined (my $kid = fork) or die "Cannot fork: $!\n";
$logger->debug("Processing $db in process $kid");
if ($kid) {
$logger->debug("Parent $$ has finished, kid's PID: $kid");
}
else {
# $r->cleanup_for_exec(); # untie the socket
open STDIN, '/dev/null' or die "Can't read /dev/null: $!";
open STDOUT, '>/dev/null' or die "Can't write: $!";
open STDERR, '>/tmp/log' or die "Can't write: $!";
# setsid or die "Can't start a new session: $!";
my $oldfh = select STDERR; local $| = 1; select $oldfh;
warn "started\n";
sleep 1, warn "$_\n" for 1..20;
warn "completed\n";
# The search would be here, for tests just add a string:
push @result, "simulate result for $db in $kid";
CORE::exit(0); # terminate the process
}
$logger->debug("End of $db");
}
$logger->debug(join(', ', @result));
return @result; # this is what I am after!
}
The problem with this code is that I don't get @result filled.
There seem to be two problems:
1. I have to use waitpid somehow and don't know how (all examples I
found just deal with one child).
2. There doesn't seem to be a way back from the forked children to
the parent because the children get their own copy of @result as
soon as they write to it (correct?)
If my analysis is correct I would like to know:
1. How to properly wait for the children and
2. How to get the results back to the main process
Is there some common storage in apache/mod_perl where the children
can write and the parent can read the results when the children are
finished? Can I use pnotes for this?
Thanks,
-Michael