minor_wazoo has asked for the wisdom of the Perl Monks concerning the following question:
Update: After flushing out the code to a full working example as per AnonomousMonk's remark, the problem does not reproduce.
I have to get back to my original code and find the error now that I have a working example.
The original question, updated to fit the updated (and working) code:
Here is my setup for parallel processing of arrays and hashes:
#!/usr/bin/env perl use strict; use warnings; use 5.30.0; use utf8; use utf8::all; use Data::Dump 'pp'; use Parallel::ForkManager; my $pal = new Parallel::ForkManager(6, '/tmp/' ); my @array = (3..56); my $array_ref = \@array; my %results; sub one { ### seting up parallel processing for six processors. my %data; my $data_ref = \%data; my $sixes = sub { my $input = shift; my $leng = int( $input / 6) + 1; my %ha; foreach (0..5) { $ha{"$_"}{'a'} = $_ * $leng; $ha{"$_"}{'b'} = ( $_ + 1 ) * $leng - 1; } $ha{"5"}{'b'} = $input; return sub { my ( $body, $finish) = @_; $pal->run_on_finish( sub { &$finish } ); foreach (0..5) { $pal->start and next; foreach ( $ha{"$_"}{'a'}..$ha{"$_"}{'b'} ) { &$body; } $pal->finish(0, \%results); } $pal->wait_all_children; } }; ## I arm (set,prime?) the closure thus: $$data_ref{'fork_s'} = $sixes->( $#$array_ref ); two($data_ref); } sub two { my $data_ref = shift; $$data_ref{'fork_s'}->( sub { my $sn = "$_"; ## = number of sentence $results{'SxW'}{"$sn"}++; # say "Results: ".pp(%results); }, sub { my $refe = $_[5]; say "Refe2 empty" if !defined $refe; if (defined($refe)) { say "Refe2 " . pp($$refe{'SxW'}); } }); } one()
The set up serves to have a routine for processing data from an array in parallel.
Testing with the exact same call from within one() worked, while in two() the data was processed correctly but did not show up in the run_on_finish section.
As stated above, now it works, so I have to go back to my code and find its problem, but confidently.
I had thought that the passing of the sub reference was playing up with Paralell::ForkManager.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Passing a sub reference which uses Parallel::ForkManager
by 1nickt (Canon) on Apr 04, 2020 at 20:58 UTC | |
by minor_wazoo (Novice) on Apr 04, 2020 at 21:35 UTC | |
by 1nickt (Canon) on Apr 05, 2020 at 16:17 UTC | |
|
Re: Passing a sub reference which uses Parallel::ForkManager
by Anonymous Monk on Apr 04, 2020 at 20:07 UTC | |
by Anonymous Monk on Apr 04, 2020 at 20:22 UTC |