【发布时间】:2015-03-26 14:03:08
【问题描述】:
sub worker {
exit 1;
}
my $thr = threads->create(\&worker);
while ($thr->is_running()) {
print "running\n";
}
my $rc = $thr->join();
exit $rc;
我来自 Python。有没有类似于尝试/除了我可以在while($thr->is_running)附近使用的东西@
我得到(因为我在工人中退出 1):
Perl exited with active threads:
0 running and unjoined
1 finished and unjoined
0 running and detached
我希望能够捕捉出口或其他编译错误工人可能有
use strict;
use warnings;
use threads;
sub worker {
my $bad = 1/0;
}
my $thr = threads->create( \&worker );
while ( $thr->is_running() ) {
sleep 1;
}
my $rc = $thr->join(); #NB - RC will be 'undef' because there's no return.
输出:
Thread 1 terminated abnormally: Illegal division by zero at mythread.pl line 6.
是否有可能捕获它的错误?以及如何以及在哪里?
【问题讨论】:
-
为什么你的线程需要退出?您应该使用
return退出线程。 -
@AKHolland 这是一个示例,因此我可以显示错误。如果我把 $a= 0 (没有我的或其他东西它会做同样的事情)。关键是我想捕捉我可能在线程中看到的任何错误
-
编译错误完全不同。如果脚本无法完整编译,则根本不会运行。此外,您还为
$a = 0选择了一个不好的例子,它是唯一可以在没有my的情况下正常工作的变量之一。 -
@AKHolland 无论如何。不是问题的重点。我希望能够处理工作线程内可能发生的任何错误
-
如果您有编译错误,它将不会运行线程(或其他任何东西)。所以,是的,这就是重点。任何正常错误 (
die "bleeeh") 都不需要特殊处理。
标签: multithreading perl