Problems with flush(). Not working as expected - is this a defect?

227 views
Skip to first unread message

Stuart King

unread,
Apr 29, 2010, 8:44:16 AM4/29/10
to Tornado Web Server
In the docs there is an async example that pulls the count of Bret's
Friendfeed stream. The last line in the docs calls finish(), which
ends the async connection as expected. I, however, don't want to end
the connection, but would prefer to flush the buffered string to the
client, and keep the connection open, so, I replace finish() with
flush():

class MainHandler(tornado.web.RequestHandler):

@tornado.web.asynchronous
def get(self):
http = httpclient.AsyncHTTPClient()
http.fetch("http://friendfeed-api.com/v2/feed/bret",
callback=self.async_callback(self.on_response))
print 'done with get'

def on_response(self, response):
if response.error: raise tornado.web.HTTPError(500)
json = tornado.escape.json_decode(response.body)
self.write("Fetched %d entries from the FriendFeed API" %
len(json['entries']))
self.flush() #THIS IS THE LINE I REPLACED

When I run curl http://localhost:8888/ with this example, I never see
the text and count written to the terminal, although the connection
remains open. Is this a bug with flush() or is there another way to
really flush the written content to the client?

Thanks in advance.

Stu

Ben Darnell

unread,
Apr 30, 2010, 8:35:29 PM4/30/10
to python-...@googlegroups.com
By default the curl command-line tool won't show any output until the
request is finished. To see the results of a partial flush you have
to pass --no-buffer to curl.

-Ben

Stuart King

unread,
May 2, 2010, 10:37:47 PM5/2/10
to Tornado Web Server
Thanks Ben.

I had a sneaky suspicion that it was the buffer and that there wasn't
enough data, and that I wasn't using curl correctly.

What I am actually trying to build is a firehose / streaming api. I
couldn't get it working using tornado (which we are using for our
API). The reason, which is obvious now, is that there wasn't actually
enough content queued. I built a custom http server with the Apache
HTTP Components, which works perfectly, but as we have everything else
on tornado, I would prefer to stick to it.

Anyway, this has cleared up my understanding.

Thanks mate.

Stu
> > When I run curlhttp://localhost:8888/with this example, I never see
Reply all
Reply to author
Forward
0 new messages