Python龙卷风服务器丢失了连接

时间:2014-07-07 15:15:33

标签: python http tornado

我有一个带有龙卷风框架的python http服务器。几次请求后,它变得不可用。在浏览器页面也不可用。在大约20秒不活动后,它会再次开始工作。

100,000个查询包含大约10个例外。在此负载下,服务器进程占用大约30%的CPU。

为什么服务器不可用?

服务器:

start_port = 4400
workers = 1

class MainHandler(tornado.web.RequestHandler):
    def get(self):
        data = ''.join(random.choice(string.ascii_uppercase + string.digits) for _ in range(1000))
        self.write(data)


def server_process(port):
    application = tornado.web.Application([
        (r"/", MainHandler),
    ])

    http_server = tornado.httpserver.HTTPServer(application)

    http_server.listen(port)
    tornado.ioloop.IOLoop.instance().start()


if __name__ == "__main__":
    for i in xrange(workers):
        port = start_port + i
        print 'process started on %d port' % port
        p = Process(target=server_process, args=(port,))
        p.start()

客户端:

def f():
    for i in xrange(500000):
        try:
            r = requests.get('http://127.0.0.1:4400')
            if i % 100 == 0:
                print i, str(r.text)
        except:
            print traceback.format_exc()
            time.sleep(5)


if __name__ == '__main__':
    for j in xrange(1):
        p = Process(target=f)
        p.start()

回溯:

Traceback (most recent call last):
  File "/home/me/PycharmProjects/test/client.py", line 16, in f
    if i % 100 == 0:
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 55, in get
    return request('get', url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/api.py", line 44, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 383, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/sessions.py", line 486, in send
    r = adapter.send(request, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/requests/adapters.py", line 378, in send
    raise ConnectionError(e)
ConnectionError: HTTPConnectionPool(host='127.0.0.1', port=4400): Max retries exceeded with url: / (Caused by <class 'socket.error'>: [Errno 99] Cannot assign requested address)

更新:

实验性地选择了硬件服务器每秒300个请求的值(不是进程)。此值不依赖于运行龙卷风的进程数。添加nginx作为代理服务器没有帮助。

服务器在ubuntu服务器12.04和linux mint 16上运行。看起来这个限制取决于debian操作系统。

1 个答案:

答案 0 :(得分:1)

问题解决了。 Multiprocessing.Process对于几个龙卷风进程来说是个坏主意。我正在使用tornado.process。