I'm working on some code to run some comm sessions in parallel, and update a database when all the sessions have completed. The database contains things like filenames, last run times, etc.

The problem I'm running into is that I'm reading the database into a hash, and dumping the hash back to the database at the end of the script. The hash copies in each fork don't update the parent's hash, and I'm left with the hash as it was at load time being re-written. Here is some dumbed-down code to illustrate:

#!/usr/bin/perl -w use strict; use Data::Dumper; use Parallel::ForkManager; my $pm = new Parallel::ForkManager(10); $pm->run_on_finish ( sub { my (undef, $exit_code, $ident) = @_; $update_flag = 1 if $exit_code; } ); my %sess_hist = (); &load_database; for my $session (keys %{$hash{Session}}) { my $pid = $pm->start($session) and next; &run_session($session) if &check_overdue($session); # &run_session updates %sess_hist with new stats else { exit (0); } $pm->finish($session); } $pm->wait_all_children; &save_database if $update_flag;

I'd be more than happy to ditch Data::Dumper in favor of a simple database, but the ones I've played with (NDBM_File, SDBM_File, etc.) all seem to do a final untie() at the end to re-write the database, putting me back in the same boat.

What's a good way out of this with the least amount of module installation?

Thanks.

Update:

Wow, that was really a "Bread good, fire bad" moment. Yes, my "database" is nothing more than a Data::Dumper printout. The easy solution ocurred to me on the drive home: Child processes create a new hash of things to change, then re-read the original hash from file, merge the changes, re-save.

Next time: more caffeine, less knee-jerk question posting.


In reply to Forking and database updates by delirium

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.