concurrency - 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;
}
}
}
我的问题是为什么它有时会失败?
问候
辛
解决方案
这个问题似乎与该方法的已知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
解决方案,我深表歉意,对此我能说的最好的就是我的描述要求更好的解决方案。
推荐阅读
- python - Python 导入错误在没有 sudo 权限的 Centos 上没有名为 _ssl 的模块
- aws-glue - 如何自定义 AWS Glue 作业的作业 ID?
- angular - 启动角度项目时错误连接被拒绝
- android - 如何在animated.ScrollView中使用带有粘性标题的FlatList
- sql - SemanticException [错误 10085]:不支持使用 LATERAL VIEW 连接 'id'
- python - 套接字创建速度差异巨大 Python3.7/Python2.7
- xamarin - 如何在 xamarin 表单的按钮内添加不同类型的 TextSize?
- sql - Oracle-apex SyntaxError : unexpected token 是什么意思?
- angular - Angular Material 2更改根后代复选框颜色
- python - 如何将数字与输入分开以添加它们?