我有一个使用线程的 Perl 脚本,如何在函数成功执行后退出函数而不退出脚本?
Perl exited with active threads:
6 running and unjoined
1 finished and unjoined
0 running and detached
我的原始脚本是单片的,没有任何功能,完成后它只是“退出”;
这就是我创建线程的方式:
for(0..$threads-1) {$trl[$_] = threads->create(\&mysub, $_);}
for(@trl) { $_->join; }
和我的潜艇:
sub mysub {
# do something and if success
exit;
}
编辑
我的完整脚本有问题:
#!/usr/bin/perl
use LWP::UserAgent;
use HTTP::Cookies;
use threads;
use threads::shared;
################################################## ######
$|=1;
my $myscript = '/alive.php';
my $h = 'http://';
my $good : shared = 0;
my $bad : shared = 0;
$threads = 10;
################################################## ######
open (MYLOG , "<myservers.log");
chomp (my @site : shared = <MYLOG>);
close MYLOG;
################################################## ######
$size_site = scalar @site;
print "Loaded sites: $size_site\n";
################################################## ######
my $browser = LWP::UserAgent->new;
$browser -> timeout (10);
$browser->agent("User-Agent=Mozilla/5.0 (Windows; U; Windows NT 5.1; ru; rv:1.8.1.11) Gecko/20071127 Firefox/2.0.0.8;" . $browser->agent);
################################################## ######
for(0..$threads-1) {$trl[$_] = threads->create(\&checkalive, $_);}
for(@trl) { $_->join; }
################################################## ######
sub checkalive {
while (@site){
{lock(@site);$url = shift @site;}
$request = $browser->get("$h$url$myscript")->as_string;
if ($request =~ /Server Alive/){open (GOOD , ">>alive.txt");print GOOD "$h$url$myscript\n"; $good++;} else {$bad++;}
print "Alive: $good Offline: $bad\r";
}
}
close GOOD;
print "Alive: $good Offline: $bad\n";