我正在尝试使用以下代码将自编写模块中的子例程传递给线程。
这是我第一次使用线程,所以我有点不熟悉它。
主脚本(短片)
#!/usr/bin/perl -w
use strict;
use threads;
use lib 'PATH TO LIB';
use goldstandard;
my $delete_raw_files = 0;
my $outfolder = /PATH/;
my %folder = goldstandard -> create_folder($outfolder,$delete_raw_files);
&tagging if $tagging == 1;
sub tagging{
my %hash = goldstandard -> tagging_hash(\%folder);
my @threads;
foreach(keys %hash){
if($_ =~ m/mate/){
my $arguments = "goldstandard -> mate_tagging($hash{$_}{raw},$hash{$_}{temp},$hash{$_}{tagged},$mate_anna,$mate_model)";
push(@threads,$arguments);
}
if($_ =~ m/morpheus/){
my $arguments = "goldstandard -> morpheus_tagging($hash{$_}{source},$hash{$_}{tagged},$morpheus_stemlib,$morpheus_cruncher)";
push(@threads,$arguments)
}
}
foreach(@threads){
my $thread = threads->create($_);
$thread ->join();
}
}
模块
package goldstandard;
use strict;
use warnings;
sub mate_tagging{
my $Referenz = shift;
my $input = shift;
my $output_temp_dir = shift;
my $output_mate_human = shift;
my $anna = shift;
my $model = shift;
opendir(DIR,"$input");
my @dir = readdir(DIR);
my $anzahl = @dir;
foreach(@dir){
unless($_ =~ m/^\./){
my $name = $_;
my $path = $input . $_;
my $out_temp = $output_temp_dir . $name;
my $out_mate_human_final = $output_mate_human . $name;
qx(java -Xmx10G -classpath $anna is2.tag.Tagger -model $model -test $path -out $out_temp);
open(OUT, "> $out_mate_human_final");
open(TEMP, "< $out_temp");
my $output_text;
while(<TEMP>){
unless($_ =~ m/^\s+$/){
if ($_ =~ m/^\d+\t(.*?)\t_\t_\t_\t(.*?)\t_\t/) {
my $tags = $2;
my $words = $1;
print OUT "$words\t$tags\n";
}
}
}
}
}
}
sub morpheus_tagging{
my $Referenz = shift;
my $input = shift;
my $output = shift;
my $stemlib = shift;
my $cruncher = shift;
opendir(DIR,"$input");
my @dir = readdir(DIR);
foreach(@dir){
unless($_ =~ m/^\./){
my $name = $_;
my $path = $input . $_;
my $out = $output . $name;
qx(env MORPHLIB='$stemlib' '$cruncher' < '$path' > '$out');
}
}
}
1;
执行此代码让我
Thread 1 terminated abnormally: Undefined subroutine &main::goldstandard -> morpheus_tagging(...) called at ... line 43.
我想我会按照我所说的方式进行调查,或者我提供参数的方式是错误的。我希望有人可以帮助我吗?我也在安全和不安全的模块上发现了一些嗡嗡声,我不确定这是否真的是问题。
我想我会按照我所说的方式进行调查,或者我提供参数的方式是错误的。我希望有人可以帮助我吗?我也发现了一些关于安全和不安全模块的事情,我不确定这是否真的是问题。谢谢提前
答案 0 :(得分:4)
您必须将子的名称或对sub的参考以及参数传递给threads->create
。所以你需要像
my $method_ref = $invoker->can($method_name);
threads->create($method_ref, $invoker, @args);
也就是说,将参数传递给threads->create
会产生一些问题,可以通过使用闭包来避免。
threads->create(sub { $invoker->$method_name(@args) })
以上内容可以更简单地写成如下:
async { $invoker->$method_name(@args) }
这让我们得到以下结论:
sub tagging {
my %hash = goldstandard->tagging_hash(\%folder);
my @jobs;
for (keys %hash) {
if (/mate/) {
push @jobs, [ 'goldstandard', 'mate_tagging',
$hash{$_}{raw},
$hash{$_}{temp},
$hash{$_}{tagged},
$mate_anna,
$mate_model,
];
}
if (/morpheus/) {
push @jobs, [ 'goldstandard', 'morpheus_tagging',
$hash{$_}{source},
$hash{$_}{tagged},
$morpheus_stemlib,
$morpheus_cruncher,
];
}
}
my @threads;
for my $job (@jobs) {
my ($invoker, $method_name, @args) = @$job;
push @threads, async { $invoker->$method_name(@args) };
}
$_->join for @threads;
}
或只是
sub tagging {
my %hash = goldstandard->tagging_hash(\%folder);
my @threads;
for (keys %hash) {
if (/mate/) {
push @threads, async {
goldstandard->mate_tagging(
$hash{$_}{raw},
$hash{$_}{temp},
$hash{$_}{tagged},
$mate_anna,
$mate_model,
);
};
}
if (/morpheus/) {
push @threads, async {
goldstandard->morpheus_tagging(
$hash{$_}{source},
$hash{$_}{tagged},
$morpheus_stemlib,
$morpheus_cruncher,
);
};
}
}
$_->join for @threads;
}
请注意,我将调用延迟到join
,直到创建完所有线程为止。你的方式是这样的,所以一次只运行一个线程。
但我们所拥有的并不是很好。我们无法限制一次有多少线程处于活动状态,而且我们(昂贵地)创建了许多线程而不是重用它们。我们可以使用工作池来解决这两个问题。
use constant NUM_WORKERS => 5;
use Thread::Queue 3.01 qw( );
my $q;
sub tagging {
my %hash = goldstandard->tagging_hash(\%folder);
my @threads;
for (keys %hash) {
if (/mate/) {
$q->enqueue(sub {
goldstandard->mate_tagging(
$hash{$_}{raw},
$hash{$_}{temp},
$hash{$_}{tagged},
$mate_anna,
$mate_model,
);
});
}
if (/morpheus/) {
$q->enqueue(sub {
goldstandard->morpheus_tagging(
$hash{$_}{source},
$hash{$_}{tagged},
$morpheus_stemlib,
$morpheus_cruncher,
);
});
}
}
}
{
$q = Thread::Queue->new();
for (1..NUM_WORKERS) {
async {
while ( my $job = $q->dequeue() ) {
$job->();
}
};
}
... call tagging and whatever ...
$q->end();
$_->join() for threads->list();
}