Link to home
Start Free TrialLog in
Avatar of bayesianmind
bayesianmind

asked on

Getting script output in realtime from mod_perl

I've got a perl script that takes a while to run but outputs helpful status messages along the way. The problem is that mod_perl is waiting until the entire script has run (10+ seconds), at which point it outputs everything. How can I make this output display to the browser as its being generated?
Avatar of Arty K
Arty K
Flag of Kazakhstan image

Try to add this statement to your perl script before any 'print':

local $|=1;

this disables output buffering
SOLUTION
Avatar of ravenpl
ravenpl
Flag of Poland image

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 bayesianmind
bayesianmind

ASKER

Noplus: Didn't seem to have any effect.

Ravnpl: I get errors when using that module: [error] Undefined subroutine &ModPerl::ROOT::ModPerl::Registry::var_www_localhost_htdocs_nds_delaytest_2epl::rflush called at /var/www/localhost/htdocs/nds/delaytest.pl

Here's my delaytest.pl:

use Apache2::RequestIO ();
print "Content-type: text/html\n\n";
print "Test message<br>";
$r->rprint("foo bar<br>");
sleep 1;
print "Test message<br>";
sleep 1;
print "Test message<br>";
sleep 1;
print "Test message<br>";
sleep 1;
print "Test message<br>";
sleep 1;
print "Test message<br>";
sleep 1;
print "Test message<br>";

Open in new window

That should be rflush on line 4 in the above example, which also errors out with that error.
bayesianmind, could you try the same example with '$|=1;'?

http://httpd.apache.org/docs/1.3/misc/FAQ.html#nph-scripts

also it may be browser itself caching response from your server.
When your perl script is running, try to listen network with 'tcpdump -w filename -s 1500 tcp port 80' or more precise filter (for your client connection only). Then look inside filename with wireshark or ethereal.
I tried adding local $|=1; and $|=1; Output is still buffered.

My browser is definitely not caching it, I tried with a empty cache. Unfortunately I can't sniff the packets because this server is on localhost. If you still think its a problem I can setup another machine to sniff them.
SOLUTION
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
Changed to text/plain, no effect there.

I sniifed lo and got the exchange, its only sending one packet back to the client.
ASKER CERTIFIED SOLUTION
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
I should also add that if you set the type to text/plain, the browser will not display it in realtime, even though it is recieving it in realtime.