我在“主”脚本中创建Perl线程,通过system
调用调用“slave”perl脚本。如果这很糟糕,请随时赐教。有时被调用的从属脚本将失败并die
。我如何在主脚本中知道这一点,以便我可以杀死主人?
有没有办法可以向主线程返回一条消息,表明从站已正确完成?我知道在线程中使用exit是不好的做法。请帮忙。
=============================================== =================================== 编辑:
为了澄清,我有大约8个线程,每个线程运行一次。它们之间存在依赖关系,因此我有阻止某些线程在初始线程完成之前运行的障碍。
系统调用也是用tee
完成的,因此这可能是返回值难以获得的部分原因。
system("((" . $cmd . " 2>&1 1>&3 | tee -a $error_log) 3>&1) > $log; echo done | tee -a $log"
答案 0 :(得分:2)
您描述问题的方式,我不认为使用线程是可行的方法。我会更倾向于分叉。无论如何,呼叫'系统'将会分叉。
use POSIX ":sys_wait_h";
my $childPid = fork();
if (! $childPid) {
# This is executed in the parent
# use exec rather than system, so that the child process is replaced, rather than
# forking a new subprocess (or maybe even shell) to run your child process
exec("/my/child/script") or die "Failed to run child script: $!";
}
# Code here is executed in the parent process
# you can find out what happened to the parent process by calling wait
# or waitpid. If you want to be able to continue processing in the
# parent process then call waitpid with second argument WNOHANG
# EG. inside some event loop, do this
if (waitpid($childPid, WNOHANG)) {
# $? now contains the exit status of child process
warn "Child had a problem: $?" if $?;
}
答案 1 :(得分:1)
可能有CPAN模块非常适合您尝试的操作。也许是Proc::Daemon - Run Perl program(s) as a daemon process.。