我正在寻找一种解决方案来运行创建自己孩子的异步子进程。
孙子进程通常运行一个shell命令,在子进程中需要返回结果。
我想使用Forks::Super
,因为它包含其他有趣的功能。
以下代码有效,但有警告称无法删除子临时文件。
我做错了什么?
use Forks::Super ':all';
$Forks::Super::CHILD_FORK_OK = 1;
use Proc::ProcessTable;
my %result;
for (;;) {
$cnt++;
if (($cnt %= 5) eq 0) { #Start child if modulo 5 eq 0 (demo trigger)
$child_pid = fork { sub => \&startChild };
$result{$child_pid} = undef; # We use only the hash key as identifier, the value is reserved for later usage
}
for my $pnt (sort keys %result) { #maintain our internal process hash
if (!&checkProcess($pnt)) { #check our running childs
print "PID $pnt has gone\n";
delete $result{$pnt};
}
}
sleep 1;
} #endless
sub startChild {
print "Starting Child $$\n";
tie my @output, 'Forks::Super::bg_qx', qq["date"];
sleep 2 + rand 7;
print "Result was $output[0]\n";
print "End Child $$\n";
}
sub checkProcess {
$tobj = new Proc::ProcessTable;
$proctable = $tobj->table();
for (@$proctable) {
if ($_->pid eq $_[0]) {
print "Found Child with PID $_[0]\n";
return 1;
}
}
return undef;
}
注释字段的输出为long,以下是最近的错误行:
Child 2767 had temp files!
/usr/local/dev/threading/.fhfork2767/README.txt
/usr/local/dev/threading/.fhfork2767/.fh_001
/usr/local/dev/threading/.fhfork2767/.fh_002.signal
/usr/local/dev/threading/.fhfork2767/.fh_003
/usr/local/dev/threading/.fhfork2767/.fh_004.signal
at /usr/local/share/perl/5.10.1/Forks/Super/Job/Ipc.pm line 3115
Forks::Super::Job::Ipc::deinit_child() called
at /usr/local/share/perl/5.10.1/Forks/Super/Job.pm line 1857
答案 0 :(得分:2)
这些只是警告信息,如果您设置$Forks::Super::CHILD_FORK_OK > 0
,它们就是多余的。
如果他们打扰你,请删除该行(Forks/Super/Job/Ipc.pm
中的第3115行)或将其更改为
Carp::cluck("Child $$ had temp files! @IPC_FILES\n")
unless $Forks::Super::CHILD_FORK_OK >= 0;
我会在下一个Forks::Super
版本中清理它。