Multithreading 如何知道线程是否在Perl中使用die
我正在“主”脚本中创建Perl线程,通过Multithreading 如何知道线程是否在Perl中使用die,multithreading,perl,Multithreading,Perl,我正在“主”脚本中创建Perl线程,通过system调用调用“从”Perl脚本。如果这是不好的,请随时启发我。有时被调用的从属脚本将失败并且死亡。我怎么能在主脚本中知道这一点,这样我就可以杀死主脚本了 是否有一种方法可以向主线程返回一条消息,指示从线程已正确完成?我理解在线程中使用exit不是一个好的做法。请帮忙 ================================================================================== 编辑: 为了澄清,
system
调用调用“从”Perl脚本。如果这是不好的,请随时启发我。有时被调用的从属脚本将失败并且死亡
。我怎么能在主脚本中知道这一点,这样我就可以杀死主脚本了
是否有一种方法可以向主线程返回一条消息,指示从线程已正确完成?我理解在线程中使用exit不是一个好的做法。请帮忙
==================================================================================
编辑:
为了澄清,我有大约8个线程,每个线程运行一次。它们之间存在依赖关系,因此我有一些障碍,阻止某些线程在初始线程完成之前运行
系统调用也是通过tee
完成的,因此这可能是返回值难以获取的部分原因。system(((“$cmd.”2>&11>&3 | tee-a$error_log)3>&1)>$log;echo done | tee-a$log”
可能有一个CPAN模块非常适合您正在尝试的操作。也许。按照您描述问题的方式,我不认为使用线程是正确的方法。我更倾向于使用fork。调用“system”无论如何都会使用fork
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 $?;
}
我选择使用线程的主要原因是因为我对某些作业有依赖关系和障碍。这可以通过fork实现吗?我对Perl中的多线程知之甚少。有没有办法让从属线程使用
系统
向主线程返回值?