in reply to Re: How to do parallel processing within mod_perl
in thread How to do parallel processing within mod_perl

http://modperlbook.org/html/10-2-Forking-and-Executing-Subprocessesfrom-mod_perl.html
  • Comment on Re^2: How to do parallel processing within mod_perl

Replies are listed 'Best First'.
Re^3: How to do parallel processing within mod_perl
by Anonymous Monk on Nov 14, 2007 at 12:11 UTC
    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)

      If you don't tell us what isn't working about the code, we probably can't guess how to fix it.

      I think you may get better help by asking on the mod_perl list, but if you do, make sure to explain how the code is failing.

        Well, as I said in my last reply: "@result is empty".

        I did some more searching and reading and it seems there are two reasons: First, I have to use waitpid somehow (don't know how to do this with multiple/unknown number of sub-processes and Second it doesn't seem to be easy to share a variable between parent and kids. I need this because the whole point is to collect search results from the children and I don't know how to get them back to the parent.

        I will write to the mod_perl list as you suggested and thanks for getting me that far.

        -Michael