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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.