In article <39E82ADC.79E61D63@biochem.usyd.edu.au>,
Joel Mackay <j.mackay@biochem.usyd.edu.au> wrote:
>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
system("dyana >/dev/null 2>&1 &");
will do what you want.
There are several things wrong with your try. The simplest is that
the
`command`
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.
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
system("dyana &");
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.
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
print "Content-type: text/html\n\n";
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.
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:
system("dyana &");
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.
The solution I suggested was:
system("dyana >/dev/null 2>&1 &");
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.
>Any suggestions out there?
Hope this helps.
Hope this helps.