因此,从评论开始 - 我对您使用的东西知之甚少,无法给您一个具体的回应,但可以提供一些通用的东西。
使用线程来“异步”你的 Perl 脚本的一部分:
#!/usr/bin/perl
use strict;
use warnings;
use threads;
use Thread::Queue;
my $input_q = Thread::Queue -> new();
my $success_q = Thread::Queue -> new();
my $failure_q = Thread::Queue -> new();
my $thread_count = 4;
sub spinoff_thread {
while ( my $target = $input_q -> dequeue() )
{
#do something to $target
my @results = `ping -c 10 -i 1 $target`;
if ( $? ) {
$failure_q -> enqueue ( $target );
}
else {
$success_q -> enqueue ( $target );
}
}
}
#main bit
for ( 1..$thread_count ) {
my $thr = threads -> create ( \&spinoff_thread );
}
foreach my $server ( "server1", "server2", "server3", "server4", "server5" ) {
$input_q -> enqueue ( $server );
}
$input_q -> end(); #will cause threads to 'bail out' because that while loop will go 'undef');
#wait for threads to complete.
foreach my $thr ( threads -> list() ) {
$thr -> join();
}
print "Fail:\n", join ("\n", $failure_q -> dequeue() ), "\n";
print "Success:\n"; join ( "\n", $success_q -> dequeue() ), "\n";
关键点是您的线程 - 基本上是子例程 - 并且可以使用队列来回传递事物。end
队列是处理告诉线程终止的好方法 - 当然还有其他方法。