【问题标题】:MultiProcessing via Perl on windowsWindows上的Perl多处理
【发布时间】:2012-02-02 08:41:53
【问题描述】:

我编写的这段代码应该打开多个进程,问题是它在 linux 上运行良好,但是当我在 windows 上执行它时,它只创建一个进程!!。这可以用 perl 在 Windows 上创建多进程吗?

$j = ARGV[0];
for($i=1; $i<=$j; $i++){
system("perl example.pl word.txt.$i &");
}

【问题讨论】:

标签: perl multiprocessing


【解决方案1】:

&amp; 是 *nix 的东西。在 Windows 中显式的 fork 可以做到这一点。

请记住,Perl 的 Windows 实现模拟使用线程分叉,所以这可能是另一种选择。

my @pids;
for my $i (1 .. $j) {

    my $pid = fork;

    unless ( $pid ) {  # Child
        system("perl example.pl word.txt.$i");
        exit 0;
    }

    push @pids, $pid;
}

waitpid $_, 0 foreach @pids;

【讨论】:

    【解决方案2】:

    最好使用封闭的 Perl 脚本中的 fork,然后在子进程中调用 system,而不使用结尾的 &amp;。父级也需要wait

    因为system 的参数是由系统shell 解析的,例如,您会在Windows shell 中遇到与在Bash 中不同的行为。

    【讨论】:

      【解决方案3】:

      使用 START 命令(Windows Batch 命令)比 fork 进程容易得多。缺点是会打开多个DOS窗口。

      system "start perl example.pl word.txt.$i";
      

      【讨论】:

        猜你喜欢
        • 2011-05-14
        • 1970-01-01
        • 2014-10-30
        • 2023-03-20
        • 2011-02-23
        • 2018-08-07
        • 2020-07-18
        • 1970-01-01
        • 2021-02-21
        相关资源
        最近更新 更多