perl主程序永远等待

jac*_*kal 3 perl

我是perl的新手,我编写了一个程序来处理一组操作系统映像上的相同操作.由于操作相同,我使用了线程.附加了该程序的缩小版本.问题是,主程序永远不会出现并等待永远.在跟踪时,我看到主程序在等待"tee"命令.我有什么东西在这里搞砸了?

我在使用Perl版本5.1的CentOS 6.7上,由于许多其他依赖项,我无法前进:(

#!/usr/bin/perl -w
use threads;


my $tee_pid= open my $tee, "|-", "tee mylog";
my @images = ( "image1" , "image2");

foreach my $image (@images){
$_ = async { do_ops_on_image() };
sleep ( 60 );
}

while( threads->list ) {
    for my $joinable ( threads->list( threads::joinable ) ) {
        $joinable->join;
    }
}
print "All thread completed \n";
close $tee;

sub do_ops_on_image
{
  my $time = `date`;
  my $id = threads->tid();
  sleep (120) if ( $id ==2 );
  print $tee "my $id started at $time \n";
}
Run Code Online (Sandbox Code Playgroud)

Dav*_*ell 7

这似乎是在版本5.14.0中修复的perl中的错误.如果你真的无法安装更新的perl(除了系统perl),那么尽量避免$ tee共享文件句柄,这是导致主线程挂起的原因.

此外,等待子线程完成的代码使用一个活动的CPU循环,它会烧掉大量的CPU.如果你只是想等到所有子线程完成,那就做类似的事情吧

my @threads;
...
for ... {
     push @threads, async { ... }
}
...
$_->join for @threads;
Run Code Online (Sandbox Code Playgroud)