I don't think the suggestions people have advanced so far are going to work. Here's a copy of a usenet article I posted a while back explaining why not.

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.

--
Mark Dominus
Perl Paraphernalia


In reply to Re: Starting a new process (fork?) by Dominus
in thread Starting a new process (fork?) by DarkBlue

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.