Link to home
Start Free TrialLog in
Avatar of sgrobins
sgrobins

asked on

Forked Process using CGI

I have a CGI program that is very time consuming and I would like to fork a process to do all the calculations while the original process will print a status message to the screen.  Once the calucations are finished, then the results will be displayed overwriting all the status messages.  The reason I need this is because when a user hits "Submit" they have ti wait a long time and therefore think nothing is happening, so they hit "Submit" again and of course it starts all over.  Can this be done?

Thanks,
Shawn
Avatar of icd
icd

Not sure if what you want can be done cleanly.

I have seen some pages that output a 'status' message as the calculation is being done, this would probably work but I think you need to cause the stdout to be 'flushed' after each status report. You would also need to be careful of outputting the status report in a table, this could cause your browser not to output any text until the end of table was reached and defeat the whole exercise.

A second alternative is to output data to a chron job or daemon that creates a page containing the data. The page the user would see would have a link to this page and a message that tells the user what time the page would be available. (I am thinking here of a process that could take several minutes, a problem I once had on a contract). The pages could be purged after, say, 24 hours.

ASKER CERTIFIED SOLUTION
Avatar of julio011597
julio011597

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sgrobins

ASKER

I have already thought of that idea, the problem is my entire results page that is created is in a table and therefore does not get shown.  I cannot change that.  I also thought of the idea of having a temporary file that has the status message in it and that auto-reloads itself say every 2 seconds, then my original data will override this file with the results (without the auto-reload mechanism).  Kinda clumsy though, cause then the URL will be changed and a temporary file would have to be created/deleted.  Hmmm.. I thought there might be a better way.
Just to go further, search engine designers have to face a similar problem; sometimes queries require a long time (exact phrases in particular)  and they don't want people go away or hit the reload button; also, they often show results formatted in tables...

The usual way requires these two steps:

1. as soon as possible make their CGI output all header information (unbuffered); i.e. the Content Type stuff and the first part of the result page - say, banners and logos, links to other pages, current search criteria, and so on; think, also, that download of images happens asynchroneously, so that the server processes make use of some of the time the user would waste anyway;

2. make each row of the result table be a table by itself, so that users see results coming as soon as they are sent back; having different tables keep equal column widths may be achieved with use of transparent images being 1 pixel of height and some needed number of pixels of width.

Would this approach fit for your needs?
That might work as a temporary solution.  Again, the problem is the very first part of the code is <table border=0 width=600>, so nothing will show up except the background image.  Basically, I mirrored www.cnet.com 's style of having the menu information on the left.  I have thought of another solution: will this work?

I have the cgi script check to see if a PID was passed in as an argument.  If not, then create a file and put a 0 in it. Then fork a child process and have the child process call the same cgi script but with the same PID as an argument.  The cgi script now sees the file with a 0 in it, so it just prints a "Processing...." and reloads every 5 seconds.  The parent process (of the original script) does all the processing and once it is finished, prints a 1 in the file with all the results information.  So, that when the child process (or actually the reloaded cgi script) sees a 1 in the file, it just prints all the results (without the reload feature).

What do you think?

Sure, i would expect it to work - BTW, i guess you are going to build your temporary files' names upon the referring PID, in order to avoid conflicts.

As a web developer, in general i'd better prefer to change my result page design, because your solution doesn't sound very elegant... but, of course, you know much more than me on the problem to be solved.
I agree, I don't like the solution either.  Definitely not elegant.  I will see what we can do about the results page and hopefully go the better / easier route.  Thanks for all the help!
Glad to have been of any help.

Good luck.