Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

fork me? fork queue!

by LanceDeeply (Chaplain)
on Sep 15, 2005 at 13:03 UTC ( #492333=note: print w/replies, xml ) Need Help??


in reply to file upload

my %children; my $max_children = 3; my @todo_list; foreach my $dir(qw/lmo lin lmf bsu ssu sst/) { fork_download($dir); } complete_downloads(); sub fork_download { my $dir = shift; if ( ( scalar keys %children ) < $max_children ) { my $pid = fork; if ( $pid ) { # this is parent process $children{$pid} = $dir; } else { if ( not defined $pid ) { die "failed to fork!\n"; } # this is child process my $link = "ftp://ftp.<Path_to_dir>.jp/$dir/*"; system("wget -nH -nd --timestamping $link"); # exit child process exit 0; } } else { # too much child labor! queue for later push @todo_list, $dir; } } sub complete_downloads { while (%children) { my $childpid = wait(); print "child [$childpid][" . $children{$childpid}. "] is done\ +n"; delete $$children{$childpid}; if ( @todo_list ) { my $nextDir= shift @todo_list; fork_download($nextDir); } } }
-HTH

Update: forgot to exit child process!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://492333]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (3)
As of 2021-05-14 23:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Perl 7 will be out ...





    Results (150 votes). Check out past polls.

    Notices?