【问题标题】:How to do parallel programming in perl on Windows?如何在 Windows 上的 perl 中进行并行编程?
【发布时间】:2017-05-19 09:55:19
【问题描述】:

我在 Unix 系统上的 perl 中使用了以下代码模式,但它在 Windows 上崩溃了。如何使用 perl 在 Windows 上使用分叉或线程来实现相同的目标?

use Parallel::ForkManager;

my $pm = Parallel::ForkManager->new($MAX_PROCESSES);

DATA_LOOP:
foreach my $data (@all_data) {
    # Forks and returns the pid for the child:
    my $pid = $pm->start and next DATA_LOOP;

    # ... do some work with $data in the child process ...

    $pm->finish; # Terminates the child process
}

【问题讨论】:

  • 使用Thread::Queue的上述代码的等价物是什么?
  • @choroba 它也没有用。也会崩溃。
  • @mob 的 Forks::Super 应该可以在 Windows 上运行。另请注意其tips for windows。试试this post
  • 听起来你的 perl 安装有一些问题。告诉更多关于崩溃的信息

标签: windows multithreading perl parallel-processing fork


【解决方案1】:

这是一个使用 fork 的示例:

#!/usr/bin/perl -w
use strict;


foreach my $data (@all_data) {
    my $pid;
    next if $pid = fork;    # Parent goes to next server.
    die "fork failed: $!" unless defined $pid;

    # From here on, we're in the child.  Do whatever the
    # child has to do...  The server we want to deal
    # with is in $data.

    exit;  # Ends the child process.
}

# The following waits until all child processes have
# finished, before allowing the parent to die.

1 while (wait() != -1);

print "All done!\n";

【讨论】:

  • 这不太一样,因为它同时产生了scalar(@all_data) 进程。 OP 的示例一次生成的进程不超过 $MAX_PROCESSES 个。
猜你喜欢
  • 1970-01-01
  • 2012-06-22
  • 2013-12-31
  • 2019-04-19
  • 1970-01-01
  • 1970-01-01
  • 2019-06-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多