http://www.perlmonks.org?node_id=379992

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

Dear monks,

I have a cgi script that does a very slow sql query, which causes a timeout. Is there any way for me to let Apache know that the script is still alive and hasn't died?

Oh, and no, I can't make the query faster (at least I don't think so) - I have used "explain", and from what I can tell MySQL seems to be using a sensible index.

Replies are listed 'Best First'.
Re: Letting Apache know we're still alive
by derby (Abbot) on Aug 04, 2004 at 14:15 UTC
    hmmm merlyn must be recuperating from OSCON ... he does have an article on that topic.
    -derby

      Good article. I had a queued request to look this up. I have an old CGI that worked for years using a timer to send activity characters back to the browser. I knew that might break with a new browser someday but the quick hack got me that initial mileage. Recently some Mac users complained that their browser doesn't get those status updates correctly and times out. merlyn's solution is the right one.

Re: Letting Apache know we're still alive
by hardburn (Abbot) on Aug 04, 2004 at 14:04 UTC

    I don't think Apache is a factor here, since it knows you're still alive because your process is alive. The problem likely is that the browser on the other end times out. There are ways to handle this, but it's hard to give specific advice without knowing more about your situation.

    Could you split the query into seperate chunks and send each bit to the browser? Could you fork off a process which will run the query while the parent sends data back to the browser?

    "There is no shame in being self-taught, only in not trying to learn in the first place." -- Atrus, Myst: The Book of D'ni.

      Hacking up the query would be very possible, actually. What it does is to select a field from one table and counting the number of rows in another table (using group by). I guess I could just select everything from the first table and then do multiple selects from the second one (sending bits back when they are ready).

      I think I'll have a go. Thanks for the idea!
Re: Letting Apache know we're still alive
by rbi (Monk) on Aug 04, 2004 at 14:00 UTC
    You can set proper values of KeepAlive and Timeout in the httpd.conf file of Apache, I guess.