plagent has asked for the wisdom of the Perl Monks concerning the following question:

my $pwm_count_hash = undef; PWM: for my $pwm (@ciona_pwm_array) { $pm->start and next PWM; ($pwm_count_hash->{$pwm}) = count_pwm_num($pwm,$seq_1,$seq_2); #print $pwm,"\t",$pwm_count_hash->{$pwm},"\n"; $pm->finish; } $pm->wait_all_children; foreach my $id (keys %{$pwm_count_hash}) { print $id,"\t",$pwm_count_hash->{$id},"\n"; # no printing here! }

I declared a hash reference ($pwm_count_hash) outside of parallel processing fork.However, when terminating all child process, this value ($pwm_count_hash) was still undefined. The foreach loop after forking processing lead to no result. I expect that this value had been defined after each child process.

I think this may be related to child-parent talk. But I have a little knowledge of it. Please let me know how to initialize $pwm_count_hash in the child process.

Replies are listed 'Best First'.
Re: value uninitilized when using Parallel::ForkManager module
by Anonymous Monk on Apr 14, 2015 at 06:51 UTC
Re: value uninitilized when using Parallel::ForkManager module
by Laurent_R (Canon) on Apr 14, 2015 at 06:37 UTC
    It seems to me that this code line:
    ($pwm_count_hash->{$pwm}) = count_pwm_num($pwm,$seq_1,$seq_2);
    is never executed, because of the next instruction of the previous line (unless $pm->start fails).

    Je suis Charlie.
      It seems to me that this code line: $pwm_count_hash->{$pwm}) = count_pwm_num($pwm,$seq_1,$seq_2); is never executed, because of the next instruction of the previous line (unless $pm->start fails).

      This is how Parallel::ForkManager works: $pm->start hides a fork() call and returns true (the child's PID) in the parent process and false in the child process. $pm->start and next makes sure that all following code is executed only in the child process, $pm->finish() at the end of the loop hides exit() and makes sure that the loop is executed only once in the child process. (Actually, Parallel::ForkManager does a little bit more. See the documentation.)

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        Thank you very much for the information, afoken, I did not know, because I never used Parallel::ForkManager, but I should probably have guessed something like that, since I wrote forking programs, albeit quite sometime ago and not in Perl.

        Je suis Charlie.
Re: value uninitilized when using Parallel::ForkManager module
by atcroft (Abbot) on Apr 14, 2015 at 18:49 UTC

    Parallel::ForkManager has a mechanism for sending data back to the parent from the child processes as of version 0.7.6, which sounds like what you are really wanting to do. (There is a section of the documentation on it, as well as an example. Adapting the example was not, in my experience, a difficult exercise.)

    Hope that helps.