Okay, I spoke too soon. Accessing a hash defined in the parent IS read/writable from run_on_finish().

my %rc_hash = (); my $MAX_PROCESSES = 120; my $pm = new Parallel::ForkManager($MAX_PROCESSES); $pm->run_on_finish( sub { my ($pid, $exit_code, $ident) = @_; print "run_on_finish: $ident (pid: $pid) exited " . "with cod +e: [$exit_code]\n"; $rc_hash{$pid} = $exit_code; } ); $pm->run_on_start( sub { my ($pid,$ident)=@_; print "** $ident started, pid: $pid\n"; } ); for(my $i=0; $i< @hosts; $i++){ $pm->start($hosts[$i]) and next; system(@some_command); $return_code = $? >> 8; $pm->finish($return_code); } foreach my $key (keys %rc_hash){ print "$key => $rc_hash{$key}\n"; }

I made an incorrect assumption.. What I was trying to do earlier is use a database handle from run_on_finish() and that didn't work, so I thought it applied to all other variables. I am not sure why accessing a hash would work, but accessing a db handle wouldn't. Is this possible?

my $dbh = DBI->connect("DBI:mysql:database=mydb;host=$DB", "user", +"pass", {'RaiseError' => 1}); my $MAX_PROCESSES = 120; my $pm = new Parallel::ForkManager($MAX_PROCESSES); $pm->run_on_finish( sub { my ($pid, $exit_code, $ident) = @_; print "run_on_finish: $ident (pid: $pid) exited " . "with +code: [$exit_code]\n"; insert_into_db(\$dbh, $pid, $exit_code); + } ); $pm->run_on_start( sub { my ($pid,$ident)=@_; print "** $ident started, pid: $pid\n"; } ); for(my $i=0; $i< @hosts; $i++){ $pm->start($hosts[$i]) and next; system(@some_command); $return_code = $? >> 8; $pm->finish($return_code); } $dbh->disconnect(); sub insert_into_db{ my $dbhdl = shift; my $pid = shift; my $ret_code = shift; $$dbhdl->do(INSERT INTO system_results ... .. ); }

If I can't use the db handle from run_on_finish(), I can still add the return code to %rc_hash and process them after $pm->finish(). Is this the right thing to do? thanks for the speedy responses. I hope I explained my problem a little better this time around.


In reply to Re^2: Parallel::ForkManager run_on_finish exit code handler by sojourn548
in thread Parallel::ForkManager run_on_finish exit code handler 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.