Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

CGI hangs unless print

by janedoh (Initiate)
on May 22, 2017 at 22:21 UTC ( [id://1190907]=perlquestion: print w/replies, xml ) Need Help??

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

Dear all,

I have a CGI script which does a lot of things such as logging in to our large company database to get information.
There is a foreach loop that goes through a list and foreach item contacts the db, gets info, etc etc. This hangs unless I
put a print "." in the foreach loop. I'm unclear why this would happen. Could it be buffering? When I flush the output
it doesn't help. It needs a print statement or it will hang.
Any ideas?

Thanks for your help.

Replies are listed 'Best First'.
Re: CGI hangs unless print
by roboticus (Chancellor) on May 23, 2017 at 02:03 UTC

    janedoh:

    Without seeing what code you're running, it's hard to come up with a possible answer. You don't need to provide everything, just pare down your application until you can reproduce the issue with a small bit of code. The nice thing about going through the exercise of trimming out unrelated parts of code is that it will frequently lead you to the answer, or at least provide you a couple nice hints.

    I don't personally work with CGI, but I'm sure that if it had a hanging problem, someone would've noticed it by now. If I had to guess (and without code, that's all I can do), I'd imagine that your process is taking a while to run, and without I/O occurring (via your print statements), the connection is timing out and disconnecting so you don't see any results.

    ...roboticus

    When your only tool is a hammer, all problems look like your thumb.

Re: CGI hangs unless print
by GrandFather (Saint) on May 23, 2017 at 02:39 UTC

    What roboticus said. CGI applications, as I'm sure you must know, work by writing their output to stdout. If your application spends a long time thinking without generating any traffic on stdout something is bound to give up with the result that at the browser end nothing seems to happen.

    Also as roboticus said, if you don't show us some code we can't help a lot. Pare down your code to just the problematic loop. If you need data, fake it. If the database takes a long time in the real code put a delay in the sample where the database code would be called. You should be able to reproduce a sketch of the code in a few dozen runable lines of code.

    Most likely you can restructure your code to remove most of the database overhead, but we can't tell without seeing at least a sketch.

    Premature optimization is the root of all job security
Re: CGI hangs unless print
by hippo (Bishop) on May 23, 2017 at 08:34 UTC
    It needs a print statement or it will hang.

    In the absence of any code, I will guess that it doesn't actually hang but instead times out. The web server and the web client will both have timeouts so that connections are not maintained indefinitely in the absence of data transfer. You can either avoid the timeouts by transferring some data (eg. the printing of dots which you've already found) or better yet you could restructure your code so that it runs in the background on the server and the client polls to determine when it has finished. The latter is generally accepted to be the better solution because it doesn't clog up the server with long-lived connections doing precious little.

    There is of course a third option which is to optimise your code so that it produces results in a reasonably short time.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1190907]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-25 11:24 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found