pepik_knize has asked for the wisdom of the Perl Monks concerning the following question:
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.
Many Thanks,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";
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.)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How do I get the child's data?
by Abigail-II (Bishop) on Jun 14, 2002 at 15:23 UTC | |
by pepik_knize (Scribe) on Jun 15, 2002 at 00:52 UTC | |
by BazB (Priest) on Jun 15, 2002 at 14:14 UTC | |
|
•Re: How do I get the child's data?
by merlyn (Sage) on Jun 14, 2002 at 15:20 UTC | |
by pepik_knize (Scribe) on Jun 15, 2002 at 00:47 UTC |