How can Perl share global variables in parallel processing?

  use Parallel::ForkManager;
  use LWP::Simple;
  my $pm=new Parallel::ForkManager(10);
  our $a =0;
 @LINK=( 10,203, 20, 20 ,20 ,10 ,101 ,01 ,10 ) ;
  for my $link (@LINK) {
    $pm->start and next;
    my $lo = ($link * 120.22 )*12121.2121212121212121*( 12121212.1212121+ $link);
    $a = $a+ $lo ;   
    print $a."\n" ; 
    $pm->finish;
  };

  print $a ; 

I tried to access a global variable in a parallel process using a parallel fork manager module. end of program global variable still remains the same .. how to achieve this? is it possible?

+3
source share
3 answers

If the program did not start parallel processes, the problem would be with the second

my $a = 0;

lines.

, , $a . , $a $a. $a - .

. IPC .

+3

, . Parallel::ForkManager fork() ( ). , , , ( perl) , , . , .

, ( ), - IPC ( ), IPC::Shareable

+9

The trick I used is to save each variable inside the fork process into a separate txt file than at the end (after fork), just go through all the files and collect them (you can delete the files if they are not needed ..

0
source

Source: https://habr.com/ru/post/1720048/


All Articles