in reply to Parent waiting on child when using modules

You are probably confused. Perl won't wait on open filehandles and if you are indeed forking properly, it won't wait on children either. A better problem description and/or actual code would help.
  • Comment on Re: Parent waiting on child when using modules

Replies are listed 'Best First'.
Re^2: Parent waiting on child when using modules
by Keystroke (Scribe) on Dec 02, 2004 at 14:55 UTC
    Well I know I'm confused. Here is a test case that will show my problem. When this is run as a cgi script, the browser will wait until the child exits before finishing. If I comment out the two Spreadsheet modules it works perfectly and finishes without waiting on the child process.
    use strict; go(); sub go { $| = 1; $SIG{CHLD} = 'IGNORE'; close STDIN; my $pid = fork; if (!defined($pid)) { print "Content-type: text/plain\n\n"; print "Could not fork!\n"; exit; } elsif ($pid) { print "Content-type: text/plain\n\n"; print "PARENT = $$, KID = $pid\n" . localtime(time); close STDERR; close STDOUT; close STDIN; exit; } else { close STDIN; close STDOUT; close STDERR; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; open(STDERR, ">test.err"); sleep(10); open(OUT,">>test") or die(); print OUT "\nKID(me) = $$ " . localtime(time); close OUT; exit; } }
      I tracked the problem down
      Spreadsheet::WriteExcel uses
      Spreadsheet::WriteExcel::Workbook which uses
      Spreadsheet::WriteExcel::Worksheet which uses
      Spreadsheet::WriteExcel::Formula which uses
      Parse::RecDescent

      and confirmed that Parse::RecDecent duplicates STDERR to 3 filehandles.

      This fixes the problem I have.
      close Parse::RecDecent::ERROR;
      close Parse::RecDecent::TRACECONTEXT;
      close Parse::RecDecent::CONTEXT;

      Is there any way to track down open filehandles in perl?