You are viewing a plain text version of this content. The canonical link for it is here.
Posted to modperl@perl.apache.org by Foo Ji-Haw <jh...@nexlabs.com> on 2005/05/31 06:23:11 UTC

[MP2] How to turn off caching?

Hi guys,

If I am not mistaken, modperl tends to cache all output until the script 
is completed, then it sends out the page. If I want to (for example) 
print a period (.) back to the browser every second, what do I need to 
do? I tried $| but it does not work.

Thanks in advance for your advice.

Re: [MP2] How to turn off caching?

Posted by Torsten Foertsch <to...@gmx.net>.
On Tuesday 31 May 2005 06:23, Foo Ji-Haw wrote:
> If I am not mistaken, modperl tends to cache all output until the script
> is completed, then it sends out the page. If I want to (for example)
> print a period (.) back to the browser every second, what do I need to
> do? I tried $| but it does not work.

My handler looks this:

package My::Content;
use Apache2::RequestRec ();
use Apache2::RequestIO ();
use Apache2::Const -compile=>qw(OK);

sub handler {
  my $r=shift;
  $r->content_type( 'text/html' );

  for( my $i=0; $i<10; $i++ ) {
    $r->print( time."\n" );
    $r->rflush;
    sleep 1;
  }
  $r->print( "\n" );
  return Apache2::Const::OK;
}

and here is what it sends every second one line as expected:

r2@opi:~> telnet localhost 8081
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
GET /mp2-handler HTTP/1.0

HTTP/1.1 200 OK
Date: Tue, 31 May 2005 09:24:41 GMT
Server: Apache/2.0.54 (Unix) mod_perl/2.0.0 Perl/v5.8.5
Connection: close
Content-Type: text/html

1117531481
1117531482
1117531483
1117531484
1117531485
1117531486
1117531487
1117531488
1117531489
1117531490

Connection closed by foreign host.

It work with simple dots as well.

Torsten

Re: [MP2] How to turn off caching?

Posted by Issac Goldstand <ma...@beamartyr.net>.
As more of a hack than a necessarily good practice, I've found that
sending a newline (in addition to $|=1) sometimes helps.  I think the
problem here is more that the browser doesn't necessarily render content
every single time some data comes in over the socket, but maybe waits
for logical looking breakpoints to render.   Someone who knows browsers
better will probably correct me, but that's what I've found.

Newlines are good because in general (unless you're in a NOBR block,
CODE block, etc ) the browser won't display it, but it seems to trigger
the browser to do something.

  Issac

Foo Ji-Haw wrote:

> Hello Rodger,
>
> Thanks for the advice. I'm concerned that this sounds like a lot of
> search-and-replace for my application. I wonder if there is a cleaner
> method that simply toggles off buffering?
>
> Anyone has any ideas on this?
>
> Rodger Castle wrote:
>
>>> If I am not mistaken, modperl tends to cache all output until the
>>> script is completed, then it sends out the page. If I want to (for
>>> example) print a period (.) back to the browser every second, what
>>> do I need to do? I tried $| but it does not work.
>>>   
>>
>>
>> I fought with this for a while, too. You can use the 'bucket brigade'
>> technique. I'm a bit green, so someone with more experience could
>> expound more.  Here's a snippet:
>>
>> sub send_response_body
>> {
>>    my ( $r, $data ) = @_;
>>       my $bb = APR::Brigade->new ( $r->pool,
>>                  $r->connection->bucket_alloc );
>>    my $b = APR::Bucket->new ( $data );
>>    $bb->insert_tail ($b);
>>    $r->output_filters->fflush ( $bb );
>>    $bb->destroy;
>> }
>>
>> Hope this helps.
>>
>> Rodger
>>
>>  
>>

Re: [MP2] How to turn off caching?

Posted by Foo Ji-Haw <jh...@nexlabs.com>.
Hello Rodger,

Thanks for the advice. I'm concerned that this sounds like a lot of 
search-and-replace for my application. I wonder if there is a cleaner 
method that simply toggles off buffering?

Anyone has any ideas on this?

Rodger Castle wrote:

>>If I am not mistaken, modperl tends to cache all output until the script 
>>is completed, then it sends out the page. If I want to (for example) 
>>print a period (.) back to the browser every second, what do I need to 
>>do? I tried $| but it does not work.
>>    
>>
>
>I fought with this for a while, too. You can use the 'bucket brigade' technique. I'm a bit green, so someone with more experience could expound more.  Here's a snippet:
>
>sub send_response_body
>{
>    my ( $r, $data ) = @_;
>    
>    my $bb = APR::Brigade->new ( $r->pool,
>				 $r->connection->bucket_alloc );
>    my $b = APR::Bucket->new ( $data );
>    $bb->insert_tail ($b);
>    $r->output_filters->fflush ( $bb );
>    $bb->destroy;
>}
>
>Hope this helps.
>
>Rodger
>
>  
>

Re: [MP2] How to turn off caching?

Posted by Rodger Castle <ro...@theartofbooks.com>.
> If I am not mistaken, modperl tends to cache all output until the script 
> is completed, then it sends out the page. If I want to (for example) 
> print a period (.) back to the browser every second, what do I need to 
> do? I tried $| but it does not work.

I fought with this for a while, too. You can use the 'bucket brigade' technique. I'm a bit green, so someone with more experience could expound more.  Here's a snippet:

sub send_response_body
{
    my ( $r, $data ) = @_;
    
    my $bb = APR::Brigade->new ( $r->pool,
				 $r->connection->bucket_alloc );
    my $b = APR::Bucket->new ( $data );
    $bb->insert_tail ($b);
    $r->output_filters->fflush ( $bb );
    $bb->destroy;
}

Hope this helps.

Rodger