Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/software/bin/perl
- # Example 2 - To get URLs in parallel when they are stored in a file.
- use Parallel::ForkManager;
- use LWP::Simple;
- my $heredoc = <<ENDS;
- # =============================================================== #
- # ERROR: Missing arguments. See usage below.
- # Usage: perl $0
- # E.g. perl $0 10 FB_inputFile.txt
- # Author: suresh.saggar\@gmail.com
- # =============================================================== #
- ENDS
- if($#ARGV != 1){
- printf $heredoc;
- exit;
- }
- my $num_of_processes = $ARGV[0];
- # Create a file handle
- open FH, "$ARGV[1]" or die "Cannot open filename[$filename]:$!\n";
- my @URLs = undef;
- chomp(@URL = ); # trim trailing spaces.
- my $pm = new Parallel::ForkManager($num_of_processes);
- #
- # Setup a callback for when a child finishes up so we can get it's exit code
- #
- $pm->run_on_finish(
- sub {
- my ($pid, $exit_code, $ident) = @_;
- print "** ID[$ident] just got out of the pool with PID[$pid] and exit code[$exit_code] \n";
- }
- );
- $pm->run_on_start(
- sub {
- my ($pid, $ident)=@_;
- print "** ID[$ident] started with PID[$pid] \n";
- }
- );
- $pm->run_on_wait(
- sub {
- print "** Have to wait for one children ...\n"
- },
- 0.5
- );
- my $id = 0;
- for my $link (@URL) {
- $pm->start($id++) and next;
- my ($fn) = $link =~ /^.*\/(.*?)$/;
- if (!$fn) {
- warn "Cannot determine filename[$fn] from link[$link]\n";
- warn "Make sure you prefix each URL with http(s):/\/\ \n";
- } else {
- $0 .= " ".$fn;
- print "Getting [$fn] from [$link]\n";
- my $rc = getstore($link, $fn);
- print "[$link] downloaded. response code:[$rc]\n";
- };
- $pm->finish;
- };
- # Close the file handle
- close FH;
- print "Waiting for Children...\n";
- $pm->wait_all_children;
- print "Everybody[$num_of_processes] is out of the pool!\n";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement