1

我正在寻找一种解决方案来运行创建自己的子进程的异步子进程。

孙子进程通常运行一个 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;
}

输出对于评论字段来说太长了,这是最近的错误行:

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 
4

1 回答 1

2

这些只是警告消息,如果你设置它们是多余的$Forks::Super::CHILD_FORK_OK > 0

如果他们打扰您,请删除该行(第 3115 行Forks/Super/Job/Ipc.pm)或将其更改为类似

Carp::cluck("Child $$ had temp files! @IPC_FILES\n")
unless $Forks::Super::CHILD_FORK_OK >= 0;

Forks::Super我会在下一个版本中清理它。

于 2013-03-05T18:18:48.757 回答