我有一个使用线程的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";
答案 0 :(得分:4)
如果您希望在退出之前完成所有线程,则需要在某些时候加入它们。
您可以在脚本结束前添加类似的内容:
for my $thread (threads->list)
{
$thread->join();
}
答案 1 :(得分:1)
更新:Tim De Lange加入主题的解决方案可能就是您想要的。但在某些情况下,以下方法可能有用。
您可以实施一些等待逻辑,以确保在退出之前一切都已完成。这是一个简单的例子:
#!usr/bin/perl
use strict;
use warnings;
use threads;
sub threaded_task {
threads->create(sub { sleep 5; print("Thread done\n"); threads->detach() });
}
sub main {
#Get a count of the running threads.
my $original_running_threads = threads->list(threads::running);
threaded_task();
print "Main logic done. Waiting for threads to complete.\n";
#block until the number of running threads is the same as when we started.
sleep 1 while (threads->list(threads::running) > $original_running_threads);
print "Finished waiting for threads.\n";
}
main();
说明: