dluxhu / perl-parallel-forkmanager

Parallel::ForkManager
20 stars 11 forks source link

How to do non blocking wait while creating child in a loop #23

Closed prashant-pokhriyal closed 5 years ago

prashant-pokhriyal commented 6 years ago

Is there any way that I can keep running parent without blocking it? One more thing I want to check in the parent process whether a child is active or its get killed. If it gets terminated or killed I've to spawn it again.

yanick commented 5 years ago

Calling $pm->start_child( sub { ... }) is non-blocking. It'll launch the child and continue to do its thing.

As for checking on the child processes, the parent has the attribute processes, which is a hashref whose keys are the pid of the children, and the values are the identifiers of those childrens (if any were given). So you can do something like:

    for my $pid ( keys $self->processes->%* ) {
        my $p = waitpid $pid, &WNOHANG;
        # $p == -1 means its gone
    }