加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

并发 – perl6 grep就像并行程序一样

发布时间:2020-12-15 21:48:51 所属栏目:大数据 来源:网络整理
导读:我在perl6中写了一个类似grep的程序,现在我把它编成了并行处理.但是我遇到了一些问题:即使使用相同的命令行,程序有时会成功,有时会失败.当它成功时,事情对我来说是正常的.当它失败时,我不知道为什么…… 这是失败时的错误消息. grep6 perl *An operation fi
我在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;
        }
    }
}

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

问候

解决方法

这个问题似乎与已知的 rakudo issue for the race method基本相同.

我转自:

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解决方案道歉,我能说的最好的是我的描述要求更好的解决方案.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读