#!/usr/bin/perl -w # # Sample snipped, containing code taken from the # documentation for Parallel::ForkManager and # example 16.21 from the _Perl_Cookbook_. # use strict; use vars qw($pm); use Parallel::ForkManager; # # Maximum of 20 simultaneous processes, # and begin graceful shutdown after 6 hrs. # my $MAX_PROCESSES = 20; my $MAX_TIME = 6 * 3600; $pm = new Parallel::ForkManager($MAX_PROCESSES) or die("Unable to create ForkManager object: $!\n"); # # &load_data() subroutine defined elsewhere # my (@all_data); @all_data = &load_data(); $SIG{ARLM} = sub { die("TimeOut"); }; eval { alarm($MAX_TIME); foreach my $data (@all_data) { my $pid = $pm->start and next; # Process or call processing routines # for $data here $pm->finish; } $pm->wait_all_children; alarm(0); }; if ($@) { if ($@ =~ m/TimeOut/) { $pm->set_max_procs(0); $pm->wait_all_children; } else { alarm(0); die; } }