I have been looking at utilizing Parallel::ForkManager in a project where I work, but I have run into a question for which I hope someone may be able to shed some light, or suggest a solution.
The project I am working on involves a process which processes information regarding a large number of devices, which means it may take several hours to collect and process all the information being used. I am looking at using P::FM as a way of handling a number of these concurrently to reduce the running time.
Because of the processing involved, however, there is a limited window of time in which the process may run without impacting other systems. Looking through the Perl Cookbook, I found example 16.21, referring to the use of eval() and signals, and thought perhaps I could combine the approaches. This is where my experience fails me as to what to expect.
Below is a snippet (haven't started writing the actual code) to give an idea of what I am thinking, but I want to confirm that there will be no weirdness that is immediately apparent, and that I am on the right track with the idea. Does this seem reasonable? Is there a better way? Is my understanding of what will happen with set_max_procs(0) correct?
Let me express my appreciation for any comments and assistance in advance.
#!/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; } }
In reply to Graceful shutdowns and Parallel::ForkManager by atcroft
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |