raz*_*z3r 4 perl multithreading
我正在编写一个简单的Perl脚本,它应该同时运行其他Perl脚本.我不知道如何使主程序等待运行线程.由于许多原因,睡眠不是一个合适的解决方案.这是我的"主要"脚本:
#!/usr/bin/perl
use threads;
main:
{
if ($#ARGV == -1) { usage(); exit(1); }
my $hostname = $ARGV[0];
my $thrssh = threads ->create(\&ssh, $hostname);
my $thrdns = threads ->create(\&dns, $hostname);
my $thrping = threads ->create(\&ping, $hostname);
my $thrsmtp = threads ->create(\&smtp, $hostname);
my $thrproxy = threads ->create(\&proxy, $hostname);
}
sub ssh {
threads->detach();
my $hostname = @_;
#print "SSH\n";
#LAUNCH SSH SCRIPT
}
#OTHER SUBROUTINES...
sub proxy {
threads->detach();
my $hostname = @_;
#print "PROXY\n";
#LAUNCH PROXY SCRIPT
}
Run Code Online (Sandbox Code Playgroud)
如果我尝试运行这个脚本,我可以注意到的第一件事是打印是"顺序的",我认为文本搞砸了,但也许打印是独家我不知道.主要问题是最后两个子程序没有时间执行.
SSH
DNS
PING
Perl exited with active threads:
2 running and unjoined
0 finished and unjoined
0 running and detached
Run Code Online (Sandbox Code Playgroud)
如果我使用join而不是分离,则子例程变为"顺序",例如,如果我在子ssh中进行休眠,则其他线程将在启动之前等待.我希望它们是并行的,主程序只在所有线程完成后关闭,任何帮助?
我实际上还有另一个问题,如果我必须在foreach cicle中运行多个线程,那么我应该加入它们?即:
my $thrssh;
foreach $mynode ($nodeset->get_nodelist) {
#...
$thrssh = threads ->create(\&ssh, $port, $hostname);
#...
}
$thssh->join();
Run Code Online (Sandbox Code Playgroud)
是对的吗?
完成join所有线程后,您需要从主线程中获取 所有线程.不detatch,如果你不想解释退出而线程仍在运行.
...
my $thrssh = threads ->create(\&ssh, $hostname);
my $thrdns = threads ->create(\&dns, $hostname);
...
$thrssh->join();
$thrdns->join();
...
Run Code Online (Sandbox Code Playgroud)
关于你的编辑:不,这是不对的.您需要保留对您创建的每个线程的引用,否则您无法加入它们.
做类似的事情:
my @thrs;
foreach $mynode ($nodeset->get_nodelist) {
#...
$thrssh = threads ->create(\&ssh, $port, $hostname);
push @thrs, $thrssh;
#...
}
$_->join() for @thrs;
Run Code Online (Sandbox Code Playgroud)