Howdy monks,

I have a script that needs to look up prices on our intranet for about 40 items. Each inquiry takes about 15-20 secs, so I'd like to fork off several children to minimize the execution time. (I'm using Linux now, but plan on using perl2exe to place it on some win98 boxes.) Using the example here: how to fork?, I've got fork working, but how do I get the data back to the parent? The data that needs to get back are: price, description, and department. I know I can write the output to a file, and have the parent read out of that, but is there a better way?

Also, I'm unclear on the $SIG{CHLD} = sub { wait; };. Is this how I can make sure that my parent waits until the children are done? It doesn't seem to be doing that as written.

forktest.pl #!/usr/bin/perl -w use strict; use LWP::Simple qw(get); my $loops = shift || 10; my $pid; open (OUT, "+> out"); print "Testing with $loops children...\n"; for my $i (0..$loops-1) { if ($pid = fork) { ## Parent $SIG{CHLD} = sub { wait; }; next; } if (defined $pid) { ## ...children my ($price,$desc,$dept); # So everyone can play along at home: my $url ="http://www.perlmonks.org/index.pl?node_id=".(6000+$i); my $page = get ($url); # extract data to $price,$desc,$dept # Now what? print OUT "$i $price $desc $dept\n"; print "$i done\n"; exit; } else { die "Fork failed at number $i: $!\n"; } } while <OUT> { # get data here? } print "I'm done\n";
Many Thanks,

Pepik

Of all the causes that conspire to blind
Man's erring judgment, and misguide the mind,
What the weak head with strongest bias rules,
Is pride, the never-failing vice of fools.
-- Pope.
(Humble to be American.)


In reply to How do I get the child's data? by pepik_knize

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.