scriptstudent has asked for the wisdom of the Perl Monks concerning the following question:
Hi All... I have perl program that used to work fine...The parent process would wait for all the children (using waitpid()) before proceeding... However, when I add LWP request command in the code before forking the child process, then the Parent process does not wait for the children to finish execution and exit...AND it also does not execute the rest of the code after waitpid statement. I have tried different ways to figure out how to make the parent to wait, but somehow the HTTP request statement is making a difference and it is not waiting for children. Any help/suggestions would be appreciated Below is the code snippet... . . .
. . . . Rest of the parent codeuse File::Copy; use Net::FTP; use Win32::Process; use File::Path; use LWP::UserAgent; use HTTP::Headers; use HTTP::Cookies; use HTTP::Response; . . . . #IMP: Below are the few request lines mainly the $res = $ua->request($ +req); statement which makes parent not to wait for the child processe +s ######## Process the URL response ua = new LWP::UserAgent; $ua->agent("$0/0.1 " . $ua->agent); $req = new HTTP::Request 'GET=> 'http://www.cpan.org/RECENT'; $req->header('Accept' => 'text/html'); # send request $res = $ua->request($req); my $url_response = $res->content; print $url_response . "\n"; print $res->status_line . "\n"; print $res->code . "\n"; . . . my @pids; # Process every Unlink item foreach $extractItem (@extractItemLines) { . . . . my $childpid = fork(); if ($childpid) { push @pids, $childpid; print "I am parent: $childpid\n\n"; } # elsif (defined $childpid) { elsif ($childpid==0) { exec($command); die "Error: Cannot exec EXTRACT command:'$command': $!\n"; print "I am child: $childpid\n\n"; } else { die "Error: Cannot fork EXTRACT process: $!\n" } ######### Check for successful execution #if ($? != 0) #{ # print "Error: $C4C_TCENG_DOWNLOAD_ITK exited with fatal error +code.\n"; # die "Error: $C4C_TCENG_DOWNLOAD_ITK exited with fatal error co +de.\n"; #} print logFP "############## Processed $extractItem ############### +#\n"; } # foreach $extractItem... #### wait for all extract processes to finish before zipping the d +irectory waitpid($_,0) for @pids;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parent process does not wait for Child processes to finish and exits after the waitpid statement
by bart (Canon) on Feb 06, 2012 at 20:04 UTC | |
by scriptstudent (Initiate) on Feb 07, 2012 at 15:42 UTC | |
|
Re: Parent process does not wait for Child processes to finish and exits after the waitpid statement
by ikegami (Patriarch) on Feb 06, 2012 at 19:30 UTC |