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.)
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.