3

I have this part of code:

como_url = "".join(['http://', options.como_address, ':', options.como_port, 
                        '/ztc_config?netid=0&opcode_group=0&opcode=0&start=-20s&end=-1s'])

http_client = AsyncHTTPClient()
response = yield tornado.gen.Task(http_client.fetch, como_url)

where I do an http request. I would add a connection timeout, to be sure that the previous code is been executed, so I can find my response.

How can I add the timeout? I have to add it into the tornado.gen.Task call? I don't know how to do.

sharkbait
  • 2,980
  • 16
  • 51
  • 89

2 Answers2

4

Use the HTTPRequest class to add a timeout to the request, instead of just passing the url to fetch. Try:

request = tornado.httpclient.HTTPRequest(url=como_url, connect_timeout=20.0, request_timeout=20.0)
response = yield tornado.gen.Task(http_client.fetch, request)

See http://www.tornadoweb.org/en/branch2.4/httpclient.html#tornado.httpclient.HTTPRequest

Neil
  • 2,378
  • 1
  • 20
  • 28
  • It doesn't work. I set the timeouts to 2.0 to fast the response, but doesn't work. – sharkbait Mar 12 '13 at 15:14
  • Connect timeout means that the request will be done after – sharkbait Mar 12 '13 at 15:19
  • With this code, if I print response, I have: `HTTPResponse(code=200,request_time=0.30609703063964844,buffer=,_body=None,time_info={},request=,effective_url='http://131.114.52.207:44444/ztc_config?netid=0&opcode_group=0&opcode=0&start=-20s&end=-1s',headers={'Content-Type': 'text/plain'},error=None)` As you can see, request_time=0....... – sharkbait Mar 12 '13 at 15:26
  • If I write `request = tornado.httpclient.HTTPRequest(url=como_url, connect_timeout=5.0, request_timeout=5.0) print request.connect_timeout` I obtain 5.0 so it works... but doesn't work! – sharkbait Mar 12 '13 at 15:58
  • you probably get this, but just to get clear: `HTTPRequest.request_timeout` is only limiting value whereas `HTTPResponse.request_time` is the actual time of finished request. – kwarunek Nov 24 '15 at 23:42
3

I have also encountered this problem, sometimes timeout doesn't work. Reason is SimpleAsyncHTTPClient.max_clients reach max value.

In SimpleAsyncHTTPClient.fetch_impl, if number of self.active greater than number of max_clients then timeout_handle be assigned none.

So you add increase tornado instance or max_clients, can solve it

Sunil Garg
  • 14,608
  • 25
  • 132
  • 189