Thanks for the pointer, I tried to follow the example as close as possible and came up with this (not proberly working) code:
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 to /dev/null
+: $!";
open STDERR, '>/tmp/log' or die "Can't write to /tmp/log:
+ $!";
# 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";
push @result, "simulate result for $db in $kid";
CORE::exit(0); # terminate the process
}
$logger->debug("End of $db");
}
$logger->debug(join(', ', @result));
}
The difference is a foreach around the forks and the
attempt to collect some result from the forked processes
in @result but @result is empty.
What am I doing wrong?
As a side-note since I am not really interested in the warn-output: /tmp/log is empty after the run. Does this indicate that the child(s) didn't run?
I left out the cleanup_for_exec and the setsid lines because from the explanation I gathered they are not necessary in my situation and I didn't know if cleanup_for_exec call is still the same in mod_perl2 (the example is for mod_perl1) |