我正在使用python发送多个HTTP请求..>同时异步触发1000个请求。但我得到了错误:达到了最大客户端限制,请求排队龙卷风,过了一段时间我得到了超时错误。
如何解决这类问题,发送多个http帖子请求并避免超时?
这是我正在使用的代码:
class AjaxBatchHandler(basehandler.BaseHandler):
@tornado.gen.coroutine
def post(self):
# just respond with a status, no redirect to login
if not self.get_current_user:
self.set_status(403)
batch = json.loads(self.get_argument("actions").encode('utf-8'))
client = tornado.httpclient.AsyncHTTPClient()
batch_requests = []
for item in batch['actions']:
request = utils.build_request(
self,
action=item['action'].replace("{API}", utils.get_api_baseurl()),
values=item['values'])
batch_requests.append(client.fetch(request))
try:
batch_responses = yield batch_requests
batch_result = dict(results=[])
for result in batch_responses:
batch_result['results'].append(json.loads(result.body))
except tornado.httpclient.HTTPError as e:
batch_result = dict(results=[])
batch_result['results'].append({"Status": 500,
"StatusMsg": e.message,
"Error": e.code
})
self.write(batch_result)
要么提高max_clients
限制(如果向您访问的网站发送更多流量是合适的),要么降低请求速度。对于前者,进行
AsyncHTTPClient.configure(None, max_clients=1000)
在你的节目开始时。对于后者,信号量或队列可用于控制发送请求的速率。看见https://github.com/tornadoweb/tornado/blob/master/demos/webspider/webspider.py