Unbuffered writes using Twiggy

54 views
Skip to first unread message

Gurunandan Bhat

unread,
May 4, 2013, 3:27:08 AM5/4/13
to psgi-...@googlegroups.com
Hi, 

I am trying to understand streaming and I wrote this:

use AnyEvent;
my $app = sub {
    return sub {
        
  my $responder = shift;
  my $writer = $responder->([ 
  200, 
  [ 'Content-Type', 'text/plain' ]
  ]);
  $writer->write("Start : ${\scalar(localtime)}\n");
  my $count = 0;
  my $w;
  $w = AE::timer 0, 5, sub {
  my $time = scalar localtime;
  $writer->write("Its now: $time\n");
  if (++$count > 10) {
  $writer->write("Stopping: $time\n");
  $writer->close;
  undef $w;
  }
  };
};
};

and ran it under Twiggy (plackup -s Twiggy). But I find that rather than getting a line after every 5 seconds, I get the entire output after 50. 

What am I doing wrong?

Thank you

Tatsuhiko Miyagawa

unread,
May 4, 2013, 3:56:56 AM5/4/13
to psgi-...@googlegroups.com
Works for me with telnet by connection to localhost 5000.
> --
>
> ---
> You received this message because you are subscribed to the Google Groups
> "psgi-plack" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to psgi-plack+...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>



--
Tatsuhiko Miyagawa

Gurunandan Bhat

unread,
May 4, 2013, 4:07:02 AM5/4/13
to psgi-...@googlegroups.com
Thanks for responding. 

This is strange. I tried telnet localhost 5000, followed by GET / and got no response. But it works fine with "curl http://localhost:5000" - prints out the time every five seconds.

Why might this be?



On Sat, May 4, 2013 at 1:36 PM, Gurunandan Bhat <gb...@pobox.com> wrote:
Thanks for responding. 

This is strange. I tried telnet localhost 5000, followed by GET / and got no response. But it works fine with "curl http://localhost:5000" - prints out the time every five seconds.

Why might this be?



Tatsuhiko Miyagawa

unread,
May 4, 2013, 4:08:46 AM5/4/13
to psgi-...@googlegroups.com
You sure you sent two returns instead of one to indicate your request is done?

Otherwise no idea, especially since you didn't provide any information
how to run it, the whole output, versions of perl, etc.

On Sat, May 4, 2013 at 1:07 AM, Gurunandan Bhat

Gurunandan Bhat

unread,
May 4, 2013, 4:15:24 AM5/4/13
to psgi-...@googlegroups.com
My apologies. I did send two returns on telnet. Here is what I get:

nandan@knuth:~/Workspace/OdysseyPay$ telnet localhost 5000
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.

HTTP/1.0 400 Bad Request
Content-Type: text/plain

I am running perl-5.16.3 under perlbrew and the latest version of Plack 1.0024 and AnyEvent 7.04.

However running with curl gives me this:
nandan@knuth:~/Workspace/OdysseyPay$ curl http://localhost:5000
Start : Sat May  4 13:34:41 2013
Its now: Sat May  4 13:34:42 2013
Its now: Sat May  4 13:34:47 2013
Its now: Sat May  4 13:34:52 2013
Its now: Sat May  4 13:34:57 2013
Its now: Sat May  4 13:35:02 2013
Its now: Sat May  4 13:35:07 2013
Its now: Sat May  4 13:35:12 2013
Its now: Sat May  4 13:35:17 2013
Its now: Sat May  4 13:35:22 2013
Its now: Sat May  4 13:35:27 2013
Its now: Sat May  4 13:35:32 2013
Stopping: Sat May  4 13:35:32 2013

And the output is correctly buffered every 5 seconds.





Gurunandan Bhat

unread,
May 4, 2013, 4:18:51 AM5/4/13
to psgi-...@googlegroups.com
More info that I missed out. My Twiggy is also the latest 0.1021 and my command line is:

nandan@knuth:~/Workspace/OdysseyPay$ plackup -s Twiggy async.psgi 
Twiggy: Accepting connections at http://0.0.0.0:5000/

Thanks for your help....

Gurunandan Bhat

unread,
May 4, 2013, 7:11:51 AM5/4/13
to psgi-...@googlegroups.com
My apologies - This works with raw telnet (I was sending the protocol string in lowercase earlier -doh!) as well as with curl. 
But when I access it with a browser (both, Chrome version 27.0.1453.15, Firefox 20.0) - the output seems to be buffered and is sent to my browser only after I call close on the writer. 

Any help would be great.

Thanks

Lee Aylward

unread,
May 4, 2013, 10:15:15 AM5/4/13
to psgi-...@googlegroups.com
In my experience, some browsers do buffer streaming responses quite a bit. My hacks have included streaming JSON with padded with spaces so it would hit 1KB for each update. The default Android browser required an even higher size. I'd suggest using WebSockets or Server Sent Events depending on your needs.

--
Lee

Gurunandan Bhat

unread,
May 4, 2013, 10:51:00 AM5/4/13
to psgi-...@googlegroups.com
You are right - I padded the output and it works!

Thanks a ton.


Reply all
Reply to author
Forward
0 new messages