首页 > 解决方案 > perl6 grep 类似程序并行

问题描述

我用perl6写了一个类似grep的程序,现在我把它变成了并行处理。但是我遇到了一些问题:即使使用相同的命令行,程序有时也会成功,有时会失败。当它成功时,事情对我来说看起来很正常。当它失败时,我不知道为什么......

这是失败时的错误消息。

> grep6 perl *
An operation first awaited:
in sub MAIN at /Users/xxx/Dropbox/bin/grep6 line 28
in block <unit> at /Users/xxx/Dropbox/bin/grep6 line 30

Died with the exception:
Cannot find method 'Any' on object of type Match
  in regex  at /Users/xxx/Dropbox/bin/grep6 line 34
  in sub do_something at /Users/xxx/Dropbox/bin/grep6 line 34
  in block  at /Users/xxx/Dropbox/bin/grep6 line 24

代码是:

#!/usr/bin/env perl6  

constant $color_red = "\e[31m";
constant $color_off = "\e[0m";

sub MAIN(Str $pattern, *@filenames){
    my $channel = Channel.new();
    $channel.send($_) for @filenames; # dir();
    $channel.close;
    my @workers;
    for 1..3 -> $n {
        push @workers, start {
            while (my $file = $channel.poll) {
                do_something($pattern, $file);
            }
        } 
    }
    await(@workers);
}

sub do_something(Str $pattern, Str $filename) {
    #say $filename;
    for $filename.IO.lines -> $line  {
        my Str $temp = $line;
        if $temp ~~ s:g/ (<$pattern>) /$color_red$0$color_off/ { 
            say $filename ~ ": " ~ $temp;
        }
    }
}

我的问题是为什么它有时会失败?

问候

标签: concurrencyraku

解决方案


这个问题似乎与该方法的已知rakudo 问题race基本相同。

我从:

if $temp ~~ s:g/ (<$pattern>) /$color_red$0$color_off/ { 

至:

if $temp ~~ s:g/ ($pattern) /$color_red$0$color_off/ { 

问题似乎消失了。

正如Xin Cheng稍后提到的并且也在同一个文档中描述的那样,更简单的插值匹配字面意思,正如文档示例所阐明的那样。问题单通过以下方式解决了问题:

my $reg = regex { <$pattern> };
'' ~~ $reg;

导致具有类似解决方法的更新程序:

#!/usr/bin/env perl6

constant $color_red = "\e[31m";
constant $color_off = "\e[0m";

sub MAIN(Str $pattern, *@filenames){
    my $channel = Channel.new();
    $channel.send($_) for @filenames; # dir();
    $channel.close;
    my @workers;    

    # match seems required for pre-compilation
    '' ~~ (my regex pat_regex { <$pattern> });

    for 1..3 -> $n {
        push @workers, start {
            while (my $file = $channel.poll) {
                do_something(&pat_regex, $file);
            }
        }
    }
    await(@workers);
}

sub do_something(Regex $pat_regex, Str $filename) {
#    say $filename;
    for $filename.IO.lines -> $line  {
        my Str $temp = $line;
        if $temp ~~ s:g/ ($pat_regex) /$color_red$0$color_off/ {
            say $filename ~ ": " ~ $temp;
        }
    }
}

对于之前提出的明确EVAL解决方案,我深表歉意,对此我能说的最好的就是我的描述要求更好的解决方案。


推荐阅读