Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I having an issue trying to get my head round 'forking'... I think! Basically I have a script executed over the Web from a form. It takes a filename as input, uploads it and then processes it. The actual processing takes an age so ideally I would like to upload the file, redirect to a web page, execute processing code in the background and finally send an email when it is all done. Currently I have something like:
open UPLOAD, ">$tmp_file"; while ( <$upload_file> ) { print UPLOAD; } close UPLOAD; print "Location: my_webpage_goes_here.htm\n\n"; my $pid = fork; $pid = 0 unless defined $pid; # be the kid if fork failed exit 0 if $pid; processing code on file goes here including subroutine to perform nslookup to verify email address &VerifyEmailAddress($email); my $mailprog = '/usr/sbin/sendmail'; open(MAIL,"|$mailprog -t"); sending email code goes here
What appears to happen is that the script does actually fork but it does not redirect to the webpage when I want it to and as it takes so long, it times out. First thing I want to fix is getting the webpage to display prior to the script forking. There are some other issues in that the forking process seems not to wait for the results of subroutines in the processing code section. I am processing email addresses and run an nslookup on addresses. I'm a little confused as to what might be happening after the forking statement? Maybe someone could shed some enlightened advice on this problem?
  • Comment on Redirecting to a webpage in a script before forking to code in a script
  • Download Code

Replies are listed 'Best First'.
Re: Redirecting to a webpage in a script before forking to code in a script
by dws (Chancellor) on May 28, 2002 at 19:37 UTC
    There are some other issues in that the forking process seems not to wait for the results of subroutines in the processing code section. I am processing email addresses and run an nslookup on addresses. I'm a little confused as to what might be happening after the forking statement?

    The problem you're running into is similar to one that merlyn addresses in a Web Techniques column on long-running tasks.

Re: Redirecting to a webpage in a script before forking to code in a script
by Abigail-II (Bishop) on May 29, 2002 at 09:34 UTC
    This problem has absolutely nothing to do with Perl. It's a matter of communication between your program and your web server. It's your web server that needs to think the program has finished. If your web server does that why watching for eof on stdout and stderr, you need to close them. Don't forget than on a fork(), filedescriptors get dupped.

    Did you know this issue if even pointed out in perldoc -f fork?

    Abigail