perl的:执行多个系统中的进程,并等待他们完成 [英] perl: executing multiple systems processes and waiting for them to finish
问题描述
目前在我的Perl脚本我做类似下面的电话:
Currently in my Perl script I make a call like the following:
system(" ./long_program1 & ./long_program2 & ./long_program3 & wait ");
我希望能够登录时,同时仍然执行这些asyncronously每个长时间运行的命令执行。我知道系统调用导致的perl,使叉子,所以是这样的可能吗?难道这由多个perl的fork()和EXEC()调用被取代?
I would like to be able to log when each of the long running commands executes while still executing them asyncronously. I know that the system call causes perl to make a fork, so is something like this possible? Could this be replaced by multiple perl fork() and exec() calls?
请帮我找到一个更好的解决方案。
Please help me find a better solution.
推荐答案
是的,当然。可以叉关闭一个子进程为每个要执行的程序的
Yes, definitely. You can fork off a child process for each of the programs to be executed.
您可以做系统()
或执行exec()
分叉,视后多少处理您需要你的Perl code系统调用完成后做(因为EXEC()在功能上系统()非常相似;退出$ RC;
)
You can either do system()
or exec()
after forking, depending on how much processing you want your Perl code to do after the system call finishes (since exec() is very similar in functionality to system(); exit $rc;
)
foreach my $i (1, 2, 3) {
my $pid = fork();
if ($pid==0) { # child
exec("./long_program$i");
die "Exec $i failed: $!\n";
} elsif (!defined $pid) {
warn "Fork $i failed: $!\n";
}
}
1 while wait() >= 0;
请注意,如果你需要做大量的叉子,你最好通过的 并行:: ForkManager
,而不是做手工分叉。
Please note that if you need to do a lot of forks, you are better off controlling them via Parallel::ForkManager
instead of doing forking by hand.
这篇关于perl的:执行多个系统中的进程,并等待他们完成的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!