龙卷风异步请求

时间:2013-03-12 11:57:12

标签: python asynchronous httprequest tornado

我在Tornado中有这部分代码:

............
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)

ret = {}
if response.error:
    ret['error'] = 'Error while retrieving the response'
    self.write(tornado.escape.json_encode(ret))
    self.finish()
else:
.........

我向传感器发送命令,然后向数据库发出http请求以从传感器检索响应。 在我做出回应并采取行动之后。

现在,我必须发送两次http请求以检索响应。我第一次向传感器发送命令时,请求太快而无法接收响应。

重新发送命令后,响应正确,一切正常。

为什么呢?如何设置我不知道请求超时...或者如何在Tornado中重新发送请求两次?

谢谢。

修改

我是这样写的:

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)
    request = tornado.httpclient.HTTPRequest(url=como_url, connect_timeout=5.0, request_timeout=2.0)
    response = yield tornado.gen.Task(http_client.fetch, request)
    print response

我获得了:

HTTPResponse(code=200,request_time=0.30609703063964844,buffer=<io.BytesIO object at 0x276a1d0>,_body=None,time_info={},request=<tornado.httpclient.HTTPRequest object at 0x2764310>,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)

为什么request_time = 0 ????

0 个答案:

没有答案
相关问题