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

Platform: Apache2 For Windows, Perl Activestate, 5.6.1

Hi, I'm trying to execute a cgi program (pl) from a parent pl and have the parent continue in its run before waiting for the child to complete. I tried calling the pl from a system call, but that only hangs. I am able to call another pl (from within the original pl) by using LWP::Simple, but it waits for the child to complete before continuing.
EX:
use LWP::Simple; $site='http://servername/scriptdir/wacka.pl; $source=getprint("$site"); print "I'm now here";
Any info on how to make the parent not wait for the child to finish or calling other cgi scripts from a parent cgi (pl)with a system call, such as system, backtics or open |, so it doesn't hang (unfortunately no error message) would be much appreciated, thanks.

2003-04-21 edit ybiC: <code> tags

Replies are listed 'Best First'.
Re: LWP Forking
by perlplexer (Hermit) on Apr 21, 2003 at 21:04 UTC
    fork() would normally be used for that and it should work on Windows if you're using ActivePerl but Win32::Process is probably more appropriate.

    --perlplexer
      It worked, thank you.
Re: LWP Forking
by iburrell (Chaplain) on Apr 21, 2003 at 21:02 UTC
    I am assuming that the parent CGI script is trying to call another CGI script on the same web server. In that case, you don't need to use LWP. The parent can execute the other CGI script directly. Although, if the parent detaches from child, then the child has no way to communicate back to the browser.

    The reason system doesn't work is that it waits for the child process to return. The solution is to use fork and then exec. When running under a web server, you must close STDERR and STDOUT, or the browser won't know that the CGI script has finished.

    if ($pid = fork()) { # parent } else { die "Cannot fork:$!" unless defined $pid; close(STDOUT); close(STDERR); exec("otherscript.pl"); }
      Hi, I tried your example, but I get a memory error - Un handled Win32 exception error in perl.exe. I have previously read that the fork function in unreliable when being used on Win32 systems.

        Threads, with which fork is emulated on Win32, have some problems in perl 5.6.1. I ran into the issue a little while ago, myself.

        If you are able to upgrade to 5.8.0, I found it works much better in this regard. Though, since you apparently got it working using Win32::Process, it's not really necessary. :)

        bbfu
        Black flowers blossom
        Fearless on my breath