我如何限制 Perl 6 中 Proc::Async 产生的并发进程数?

How do I limit the number of concurrent processes spawned by Proc::Async in Perl 6?

我想在我的脚本中处理一个子任务中的文件列表,我正在使用 Proc::Async 来生成执行该工作的子进程。缺点是如果我有大量文件要处理,它会产生许多子进程。我想知道如何限制 Proc::Async 生成的并发子进程的数量?

您可以使用 Jonathan Worthington 在 his concurrency/parallelism/asynchrony talk at the 2019 German Perl Workshop 中演示的 React 块技术明确限制 Proc::Async 进程的数量(例如,请参见幻灯片 39)。我在下面的代码中使用 Linux 命令 echo N 作为我的 "external process"。

#!/bin/env perl6    
my @items = <foo bar baz>;

for @items -> $item {
    start { say "Planning on processing $item" }
}

# Run 2 processes at a time
my $degree = 2;

react {
    # Start $degree processes at first
    run-one-process for 1..$degree;

    # Run one, run-one again when it ends, thus maintaining $degree active processes at a time
    sub run-one-process {
        my $item    = @items.shift // return;
        my $proc    = Proc::Async.new('echo', "processing $item");
        my @output;

        # Capture output
        whenever $proc.stdout.lines { push @output, $_; }

        # Print all the output, then start the next process
        whenever $proc.start {
            @output.join("\n").say;
            run-one-process
        }
    }
}

旧答案:

根据 Jonathan Worthington 的演讲 Perl 6 中的并行性、并发性和异步性 (video, slides),这听起来最像 parallelism(即选择一次做多件事;见幻灯片 18)。 异步是对未来的事情做出反应,我们无法控制时间;请参阅幻灯片 39 和 40。正如@raiph 在他的评论中指出的那样,您可以拥有一个、另一个或两者。

如果你关心结果的顺序,那么使用hyper, but if the order isn't important, then use race

在这个改编自 Jonathan Worthington's slides 的示例中,您构建了一个步骤流水线,其中使用 4 个工作程序以 32 个文件名为一组处理数据:

sub MAIN($data-dir) {
    my $filenames = dir($data-dir).race(batch => 32, degree => 4);
    my $data = $filenames.map(&slurp);
    my $parsed = $data.map(&parse-climate-data);
    my $european = $parsed.grep(*.continent eq 'Europe');
    my $max = $european.max(by => *.average-temp);
    say "$max.place() is the hottest!";
}