[Chicago-talk] Script design question

Mike Fragassi frag at ripco.com
Wed Dec 14 07:37:56 PST 2005

On Tue, 13 Dec 2005, Young, Darren wrote:

> The basic need is to launch 4 system commands (at the same time) and
> watch each one for completion and when one is done to start another.
> Basically, it needs to keep 4 child processes running up to a defined
> end.

This is one of those cases where there is a relatively simple CPAN module
that does this exact thing, and it does it well.  It's called
Parallel::ForkManager.  Basically, it works like this:

  my $forker = new Parallel::ForkManager(4);
  # The numeric arg = number of simultaneous child processes you want.

  ## Set up callback functions that are called when each child process
  ## starts and finishes.
  $forker->run_on_start( sub { my ($pid) = @_;
                               # Code for logging the start of a new
                               # child process can go here.
                             } );
  $forker->run_on_finish( sub { my ($pid, $exit_code) = @_;
                               # Code for logging the end of a
                               # child process can go here.
                              } );

  ## Spawn children, keeping 4 running.
  for my $group (@groups) {
      my $pid = $forker->start() and next;

      ## Rest of loop is the code that each forked child
      ## process should do with its $group.  In your case, run the
      ## imsbackup and get the exit code.
      ## Let's assume you've stored the imsbackup exit code
      ## in the variable $exit_code:

      $forker->finish($exit_code);  # closes process

That's it. The whole thing uses waitpid(), but wraps it up so you never
have to directly deal with it.

In the callbacks, the pid of the child is always passed in, along with
whatever variables you pass in to start() or finish().  In the example
that's what I've done with $exit_code for finish().

I've been using Parallel::ForkManager for the last few years; if you use
it and need help, let me know.

POE should work too, but it's much more complicated.

-- Mike F.

More information about the Chicago-talk mailing list