Perl如何在并行处理中共享全局变量? [英] How can Perl share global variables in parallel processing?

查看:252
本文介绍了Perl如何在并行处理中共享全局变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  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 ; 

我试图使用并行fork管理器模块在并行进程上访问全局变量.程序结束时,全局变量仍然保持不变..如何实现呢?是否可能?

I was trying to access the global variable on parallel process using parallel fork manager module . end of the program the global variable still remaining same .. how to achieve this ? whether its is possible ?

推荐答案

如果程序未启动并行进程,则问题可能出在第二个

If the program wasn't starting parallel processes, then the problem would be with the second

my $a = 0;

行.

但是,由于您正在启动并行进程,因此每个$a都将位于其内存空间中.这意味着每个$a是第一个$a的副本.因此,最后一个第一个$a永远不会改变.

However, because you are starting parallel processes, each $a will be in it's memory space. That means each $a is a copy of the first $a. And the last first $a will never change, because of that.

从一个进程到另一个进程获取值需要一些进程间通信.这可以通过 sockets IPC 或其他某种机制来完成.

Getting a value from one process to another process takes a bit of interprocess communication. This can be done with sockets or IPC, or some other mechanism.

这篇关于Perl如何在并行处理中共享全局变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆