在Tornado中将超时设置为http请求 [英] Set timeout to an http request in Tornado

查看:106
本文介绍了在Tornado中将超时设置为http请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这部分代码:

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)

我在哪里执行http请求.我将添加一个连接超时,以确保先前的代码已执行,因此我可以找到响应.

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.

如何添加超时时间?我必须将其添加到tornado.gen.Task调用中?我不知道该怎么办.

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

推荐答案

使用 HTTPRequest 类向请求添加超时,而不是仅将URL传递给 fetch .试试:

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)

请参见 http://www.tornadoweb.org/en/branch2.4/httpclient.html#tornado.httpclient.HTTPRequest

这篇关于在Tornado中将超时设置为http请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆