How to set a timeout for AsyncHTTPClient?

4,805 views
Skip to first unread message

moonspirit

unread,
Aug 4, 2011, 1:48:11 AM8/4/11
to Tornado Web Server
hi, guys,

Can i set a timeout in AsyncHTTPClient?
That means, if we didn't get the fetch response in configured
time, my callback will get called with timeout error info.

simp x

unread,
Aug 4, 2011, 3:38:33 AM8/4/11
to python-...@googlegroups.com
see
httpclient = tornado.httpclient.AsyncHTTPClient()
httprequest = tornado.httpclient.HTTPRequest(url,headers=final_headers,method=method,body=data,connect_timeout=240.0)
you can set the connect_timeout

2011/8/4 moonspirit <moonspi...@gmail.com>:

yuexuan chen

unread,
Aug 4, 2011, 8:22:01 AM8/4/11
to python-...@googlegroups.com
Just found that connect_timeout and request_timeout didn't work as expected.

i have set both connection_timeout and request_timeout to 1, i can still get the http response after 150ms.


2011/8/4 simp x <sim...@gmail.com>



--
Best Regards,
yuexuan

Ben Darnell

unread,
Aug 4, 2011, 1:14:25 PM8/4/11
to python-...@googlegroups.com
The timeouts are in seconds - use 0.1 to set a 100ms timeout.  If you only set one of the timeout variables, use request_timeout, which governs the whole request, instead of connect_timeout, which only covers the initial connection.  

-Ben

yuexuan chen

unread,
Aug 17, 2011, 5:51:53 AM8/17/11
to python-...@googlegroups.com
set the timeout to 0.1 won't work, i see the following line:

    curl.setopt(pycurl.CONNECTTIMEOUT, int(request.connect_timeout))
    curl.setopt(pycurl.TIMEOUT, int(request.request_timeout))

if we set it to 0.1, it will pass 0 to pycurl and will never get timeout.

So we must set the timeout above 1 if we need.


2011/8/5 Ben Darnell <b...@bendarnell.com>



--
Best Regards,
yuexuan

Ben Darnell

unread,
Aug 17, 2011, 1:12:28 PM8/17/11
to python-...@googlegroups.com
You should use SimpleAsyncHTTPClient instead of CurlAsyncHTTPClient (this has been the default since tornado 2.0).  It should support fractional timeouts correctly.

-Ben

Li Weijian

unread,
Jun 11, 2013, 11:39:16 AM6/11/13
to python-...@googlegroups.com, b...@bendarnell.com
I was wondering how to set  timeout for AsyncHTTPClient nowadays?

I wrote a testing web server for network latency such like this:
class SlowHandler(victimweb.RequestHandler):
    def post(self):
        sleep(5)
        self.write("done");

Then I use AsyncHTTPClient to fetch this url, and encountering a lot of  `HTTPError: HTTP 599: Timeout `

在 2011年8月18日星期四UTC+8上午1时12分28秒,Ben Darnell写道:

Ben Darnell

unread,
Jun 11, 2013, 10:36:52 PM6/11/13
to Li Weijian, Tornado Mailing List
On Tue, Jun 11, 2013 at 11:39 AM, Li Weijian <liweiji...@gmail.com> wrote:
I was wondering how to set  timeout for AsyncHTTPClient nowadays?

I wrote a testing web server for network latency such like this:
class SlowHandler(victimweb.RequestHandler):
    def post(self):
        sleep(5)
        self.write("done");

If you sleep in a tornado application, nothing else can happen on the IOLoop while the thread is asleep.  You need to use asynchronous methods like IOLoop.add_timeout instead of time.sleep.

-Ben

Li Weijian

unread,
Jun 12, 2013, 5:25:35 AM6/12/13
to python-...@googlegroups.com, Li Weijian, b...@bendarnell.com
Thanks Ben, I got it

class SlowHandler(tornado.web.RequestHandler):
    #def post(self):
    @tornado.web.asynchronous
    def get(self):
        self.get_data(callback=self.on_foolish);
    def get_data(self, callback):
        num = randint(3,8)
        tornado.ioloop.IOLoop.instance().add_timeout(time()+num,
                lambda: callback(num))
    def on_foolish(self, data):
        self.write("Sleep for %d seconds" % data)
        self.finish()


在 2013年6月12日星期三UTC+8上午10时36分52秒,Ben Darnell写道:
Reply all
Reply to author
Forward
0 new messages