【问题标题】:Perl hand Module to threadsPerl 手模块到线程
【发布时间】:2017-12-01 17:48:28
【问题描述】:

我正在尝试使用以下代码将子程序从自写模块传递到线程。

这是我第一次使用线程,所以我有点不熟悉。

主脚本(缩写)

#!/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.

我猜我调用踏板的方式或我提供参数的方式是错误的。我希望有人可以帮助我吗?我还在安全和不安全模块上发现了一些东西,但我不确定这是否真的是问题所在。

我猜我调用踏板的方式或我提供参数的方式是错误的。我希望有人可以帮助我吗?我还在安全和不安全模块上发现了一些东西,但我不确定这是否真的是问题。提前致谢

【问题讨论】:

  • The documentation of the threads pragma 明确表示create 的第一个参数必须是包含为线程提供代码的子例程名称的字符串,或者是对该子例程的引用。任何后续参数都作为调用参数直接传递给子例程。您已经传递了一串未编译的 Perl 代码,这不是这些选项中的任何一个。此外,goldstandard -&gt; morpheus_tagging 是一个类方法调用,它将字符串goldstandard 作为第一个参数传递。

标签: multithreading perl perl-module


【解决方案1】:

您必须将子名称或对子的引用以及参数传递给threads-&gt;create。所以你需要类似的东西

my $method_ref = $invoker->can($method_name);
threads->create($method_ref, $invoker, @args);

也就是说,将参数传递给 threads-&gt;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();
}

【讨论】:

  • 这太棒了。谢谢!
  • 如我所见,$q->enqueue(sub {... . 但是,如果单独使用它,它就会被定义和工作。
猜你喜欢
  • 2011-11-24
  • 2014-03-01
  • 2020-06-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多