in reply to Starting a new process (fork?)
In article <39E82ADC.79E61D63@biochem.usyd.edu.au>, Joel Mackay <j.mackay@biochem.usyd.edu.au> wrote:Hope this helps.
>I tried simply using:
>
>`dyana &`
>and a number of variations thereof, but the browser always waits till
>the program is finished running before continuing.
I suspect that
will do what you want.system("dyana >/dev/null 2>&1 &");There are several things wrong with your try. The simplest is that the
notation specifically instructs Perl to wait until it has received all the data from the command. Even though you 'ran the command in the background', you sabotaged that by forcing the main program to wait until it had received all the command's output.`command`Because of this sort of situation, is is usually considered bad style to use `...` unless you are interested in getting the output of the command back into the master program. If you are not interested in the command output, you should probably use system instead.
Using
will fix this part of the problem. However, in the case of CGI programs, that is not usually enough to get the browser to continue. Here's what is happening.system("dyana &");The browser sends the request to the server. The server runs your CGI program. It is waiting for the program to complete; then it will gather all the output data from the command, package them up, and send the package back to the browser. The browser will display the data and return control to the user.
The server will not send the package until it has all the data from the command. It is attached to the CGI program via a pipe which is hooked into the standard output of the program. When the CGI program prints data to the standard output, as with
or whatever, the string actually goes into the pipe, and is received by the server. The server is waiting for this pipe to be closed. When the pipe is closed, the server knows that nor more information is forthcoming, so it can send all the data off over the network to the browser. Until the pipe closes, there might be more data, to the server must wait.print "Content-type: text/html\n\n";Pipes close automatically when there is nobody left to write to them. Normally, the CGI program is the only thing attached to the writign end of the pipe, so when it exits, the pipe closes and the server sends the data.
However, when a process runs a subprocess, the subprocess inherits all the open files and pipes from its parent. If you did:
then dyana would inherit the pipe back to the server. When the main CGI program exited, dyana would still be attached to the pipe, so it wouldn't close, and the server would continue to wait for the end of the command output. This wouldn't occur until dyana had also exited or otherwise closed STDOUT.system("dyana &");The solution I suggested was:
The >/dev/null detaches dyana's standard output from the pipe and points it into /dev/null. 2>&1 means "make the standard error go to the same place that standard output is going to"---in this case, /dev/null. (2>&1 may be unnecessary, but some web servers attach standard error to the server pipe also.) The & puts the command in the background.system("dyana >/dev/null 2>&1 &");>Any suggestions out there?
Hope this helps.
--
Mark Dominus
Perl Paraphernalia
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re (tilly) 2: Starting a new process (fork?)
by tilly (Archbishop) on Apr 17, 2001 at 17:26 UTC | |
|
Re: Re: Starting a new process (fork?)
by DarkBlue (Sexton) on Apr 17, 2001 at 16:48 UTC | |
by Zoogie (Curate) on Apr 17, 2001 at 17:27 UTC | |
by tye (Sage) on Apr 17, 2001 at 18:55 UTC |