简体   繁体   English

如何增加龙卷风超时

[英]how to increase timeout in tornado

I'm trying tornado framework. 我正在尝试龙卷风框架。 But I found tornado frequently failed after 30 seconds in the stress test (using multi-mechanize). 但是我发现龙卷风在压力测试(使用多机械化)的30秒后经常失败。 I use 10 threads in multi-mechanize and run for 100 seconds, around 500 requests / seconds. 我在多机械化中使用10个线程,并运行100秒,大约500个请求/秒。 And it's around 15% failure ratio after 30 seconds. 30秒后故障率约为15%。 The whole test is about 100 seconds. 整个测试大约需要100秒。 From the statistics, I realize, the failure may due to timeout after 0.2 seconds. 从统计数据中,我意识到失败可能是由于0.2秒后超时所致。 I searched for several ways to increase the timeout on web, but nothing works. 我搜索了几种增加网络超时的方法,但是没有任何效果。

The below is my tornado code: 以下是我的龙卷风代码:

import tornado.ioloop
import tornado.web

class MainHandler(tornado.web.RequestHandler):
    @tornado.web.asynchronous
    def get(self):
        self.write("Hello, world")
        self.finish()

application = tornado.web.Application([
    (r"/", MainHandler),
])

if __name__ == "__main__":
    application.listen(8000)
    tornado.ioloop.IOLoop.instance().start()

Here is my multi-mechanize test script: 这是我的多机械化测试脚本:

import requests
import random
import time

class Transaction(object):
    def __init__(self):
        pass
    def run(self):
        r = requests.get('http://127.0.0.1:8000')
        output = r.raw.read()
        assert(r.status_code == 200)
        return output

if __name__ == '__main__':
    trans = Transaction()
    trans.run()
    print trans.custom_timers

The following is the error message I got from multimech-run 以下是我从multimech-run获得的错误消息

Traceback (most recent call last):
  File "././test_scripts/v_user.py", line 12, in run
    r = requests.get('http://127.0.0.1:8000')
  File "/Library/Python/2.7/site-packages/requests/api.py", line 54, in get
    return request('get', url, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/safe_mode.py", line 37, in wrapped
    return function(method, url, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/api.py", line 42, in request
    return s.request(method=method, url=url, **kwargs)
  File "/Library/Python/2.7/site-packages/requests/sessions.py", line 230, in request
    r.send(prefetch=prefetch)
  File "/Library/Python/2.7/site-packages/requests/models.py", line 603, in send
    timeout=self.timeout,
  File "/Library/Python/2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 415, in urlopen
    body=body, headers=headers)
  File "/Library/Python/2.7/site-packages/requests/packages/urllib3/connectionpool.py", line 267, in _make_request
    conn.request(method, url, **httplib_request_kw)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 941, in request
    self._send_request(method, url, body, headers)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 975, in _send_request
    self.endheaders(body)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 937, in endheaders
    self._send_output(message_body)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 797, in _send_output
    self.send(msg)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 759, in send
    self.connect()
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/httplib.py", line 740, in connect
        self.timeout, self.source_address)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 571, in create_connection
    raise err
error: [Errno 49] Can't assign requested address

To answer your question of how to change the timeout for tornado, you need to modify tornado's bind_socket function to set the time out: http://www.tornadoweb.org/documentation/_modules/tornado/netutil.html 要回答有关如何更改龙卷风超时的问题,您需要修改龙卷风的bind_socket函数以设置超时时间: http : //www.tornadoweb.org/documentation/_modules/tornado/netutil.html

sock.setblocking(0)
sock.bind(sockaddr)
sock.listen(backlog)
sockets.append(sock)

Change the first line to sock.setblocking(1). 将第一行更改为sock.setblocking(1)。 According to the documentation: http://docs.python.org/library/socket.html#socket.socket.settimeout : 根据文档: http : //docs.python.org/library/socket.html#socket.socket.settimeout

Setting a timeout of None disables timeouts on socket operations s.settimeout(None) is equivalent to s.setblocking(1). 将超时设置为None将禁用套接字操作的超时s.settimeout(None)等同于s.setblocking(1)。

However, as suggested in the comment, I think you should look at distributing the load. 但是,正如评论中建议的那样,我认为您应该看看分配负载。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM