thanks, I am using slightly modified version of the last snippet of code, but it seems like the callback function run_on_finish() doesn't get called for SOME of the forked processes.

The processes are definitely spawned and the external commands are run, and I can see the output on STDOUT. I am spawning about 50 processes at a time, every 10 seconds. More precisely, I am spawning 1 process every 10 seconds, which then in turn forks 50 processes to do the work.

If run_on_finish() is never called, then I would think it's due to some programmatic error, but it seems to be sporadic. Which of course doesn't rule out error on my part, but probably a more subtle error that has left me a little puzzled.

Here's the simplified code structure:

$SIG{'ALRM'} = 'sig_handler'; setitimer(ITIMER_REAL, 1, 10); while (1){} sig_handler{ if ($pid = fork){ }else{ my $MAX = 1000; # just for testing my $pm = new Parallel::ForkManager($MAX); my $dbh = DBI->connect("DBI:mysql:database=xxxxxx;host=SOME +HOST", "user", "passwd", {'RaiseError' => 1}); $pm->run_on_finish( sub { my ($pid, $exit_code, $ident) = @_; my ($check_id, $host) = $ident =~ /^(.*?) on (.*)/s; insert_result(\$dbh, $host, $check_id, $exit_code); } ); $pm->run_on_start( sub { my ($pid,$ident)=@_; print "** $ident started, pid: $pid\n"; } ); for(my $i=0; $i< @servers; $i++){ my $srv_id = $servers[$i]{id}; my $srv_name = $servers[$i]{name}; for my $check_id (@{$checks{$srv_id}}){ $pm->start("$check_id on $srv_id") and next; exec(@{$commands{$check_id}}); print(STDERR "$check_id on $srv_name exec failed: $!\ +n"); _exit($!); } } $dbh->disconnect(); exit(); } }

Please let me know if you have any suggestions. The reason that I am spawning a child to call Parallel:FM, is that I want to ensure that a process is spawned exactly every 10 seconds to do the work of spawning processes to run on process on X number of hosts, where X can grow depending on what it can scale to (with some experimenting). thanks.


In reply to Re^2: Forking child processes. by sojourn548
in thread Forking child processes. by sojourn548

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.