The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Proc::Branch - Creating Multiple Child Processes and Merging

SYNOPSIS

  use Proc::Branch;
  my $b = Proc::Branch->new( branch => 4 ); # 1 parent and 3 children
  my $procid = $b->proc;                    # serial number of the process
  print "I am processor $procid.\n";
  if ( $procid == 0 ) {
      print "I am the parent.\n";
      for ( 1 .. 3 ) {
          my $pid = $b->pid($_);            # PID of the children
          print "I have child $pid\n";
      }
  }
  $b->merge;                                # merging the branched processes
  $b = undef;                               # same as above

DESCRIPTION

This module branches the current process into multiple processes when the object is created. Internally, perl function "fork" is used.

METHODS

new

new is the constructor method. It has arguments shown below.

  $b = Proc::Branch(
      # default values
      'branch' => 2,     # number of branches including the parent process
      'sleep'  => 0,     # sleep time between forking
      'debug'  => 0,     # turn on to see detailed messages
      'auto_merge' => 1, # When the object is destroyed, it merges.
      # When auto_merge is turned off,
      # 'merge' should be called somewhere.
  );
proc

proc returns serial number of the processes. Parent process is 0.

pid(serial_number)

pid returns process ID of the child with the serial_number. When it is called by a child process, undef is returned.

merge

Branched processes are merged. If auto_merge mode, you can simply destroy the object to call this method.

SEE ALSO

Proc::Fork, Proc::Simple

AUTHOR

In Suk Joung, <jmarch@hanmail.net>

COPYRIGHT AND LICENSE

Copyright (C) 2009 by In Suk Joung

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.5 or, at your option, any later version of Perl 5 you may have available.