Skip to main content
 首页 » 编程设计

python之结合 Tornado 和 zmq ioloops : Connection reset by peer exception

2025年05月04日13shangdawei

当我尝试按照 pyzmq official documentation 中的描述组合 Tornado 和 pyzmq ioloops 时,我遇到了一个恼人的(但并不严重)问题.

我有一个运行 tornado (T) 服务器的进程,它接受来自客户端 (C) 的 REST API 请求,并通过 ZMQ 传输代理它们到另一个真正工作的进程 (Z)。

C <-> T <-> Z 

如果 C 在 Z 回复 T 之前 关闭连接,Z( Tornado )会输出一长串异常跟踪(见底部)。想象一下以下示例:

import tornado.ioloop 
from tornado.web import Application, RequestHandler, asynchronous 
from zmq.eventloop import ioloop 
import time 
 
def time_consuming_task(): 
    time.sleep(5) 
 
class TestHandler(RequestHandler): 
    def get(self, arg): 
        print "Test arg", arg 
        time_consuming_task() 
        print "Ok, time to reply" 
        self.write("Reply") 
 
if __name__ == "__main__": 
    app = tornado.web.Application( 
        [ 
            (r"/test/([0-9]+)", TestHandler)  
        ]) 
 
    ioloop.install() 
    app.listen(8080) 
    tornado.ioloop.IOLoop.instance().start() 

这个例子实际上并没有与任何 ZMQ peer 对话,它只是将 pyzmq ioloop 附加到 tornado 的 ioloop。不过,这足以说明问题。

从控制台一运行服务器:

% python example.py 

从控制台 2 运行客户端并在服务器回复之前中断它(即在 5 秒内):

% curl -is http://localhost:8080/test/1 
^C  

服务器的输出是:

Test arg 1 
Ok, time to reply 
WARNING:root:Read error on 24: [Errno 54] Connection reset by peer 
ERROR:root:Uncaught exception GET /test/1 (::1) 
HTTPRequest(protocol='http', host='localhost:8080', method='GET', uri='/test/1', version='HTTP/1.1', remote_ip='::1', body='', headers={'Host': 'localhost:8080', 'Accept': '*/*', 'User-Agent': 'curl/7.21.4 (universal-apple-darwin11.0) libcurl/7.21.4 OpenSSL/0.9.8r zlib/1.2.5'}) 
Traceback (most recent call last): 
  File "/Library/Python/2.7/site-packages/tornado/web.py", line 1023, in _execute 
    self.finish() 
  File "/Library/Python/2.7/site-packages/tornado/web.py", line 701, in finish 
    self.request.finish() 
  File "/Library/Python/2.7/site-packages/tornado/httpserver.py", line 433, in finish 
    self.connection.finish() 
  File "/Library/Python/2.7/site-packages/tornado/httpserver.py", line 187, in finish 
    self._finish_request() 
  File "/Library/Python/2.7/site-packages/tornado/httpserver.py", line 223, in _finish_request 
    self.stream.read_until(b("\r\n\r\n"), self._header_callback) 
  File "/Library/Python/2.7/site-packages/tornado/iostream.py", line 153, in read_until 
    self._try_inline_read() 
  File "/Library/Python/2.7/site-packages/tornado/iostream.py", line 386, in _try_inline_read 
    if self._read_to_buffer() == 0: 
  File "/Library/Python/2.7/site-packages/tornado/iostream.py", line 421, in _read_to_buffer 
    chunk = self._read_from_socket() 
  File "/Library/Python/2.7/site-packages/tornado/iostream.py", line 402, in _read_from_socket 
    chunk = self.socket.recv(self.read_chunk_size) 
error: [Errno 54] Connection reset by peer 
ERROR:root:Cannot send error response after headers written 
ERROR:root:Uncaught exception, closing connection. 
Traceback (most recent call last): 
  File "/Library/Python/2.7/site-packages/tornado/iostream.py", line 304, in wrapper 
    callback(*args) 
  File "/Library/Python/2.7/site-packages/tornado/httpserver.py", line 262, in _on_headers 
    self.request_callback(self._request) 
  File "/Library/Python/2.7/site-packages/tornado/web.py", line 1412, in __call__ 
    handler._execute(transforms, *args, **kwargs) 
  File "/Library/Python/2.7/site-packages/tornado/web.py", line 1025, in _execute 
    self._handle_request_exception(e) 
  File "/Library/Python/2.7/site-packages/tornado/web.py", line 1065, in _handle_request_exception 
    self.send_error(500, exc_info=sys.exc_info()) 
  File "/Library/Python/2.7/site-packages/tornado/web.py", line 720, in send_error 
    self.finish() 
  File "/Library/Python/2.7/site-packages/tornado/web.py", line 700, in finish 
    self.flush(include_footers=True) 
  File "/Library/Python/2.7/site-packages/tornado/web.py", line 660, in flush 
    self.request.write(headers + chunk, callback=callback) 
  File "/Library/Python/2.7/site-packages/tornado/httpserver.py", line 429, in write 
    self.connection.write(chunk, callback=callback) 
  File "/Library/Python/2.7/site-packages/tornado/httpserver.py", line 177, in write 
    assert self._request, "Request closed" 
AssertionError: Request closed 
ERROR:root:Exception in callback  
Traceback (most recent call last): 
  File "/Library/Python/2.7/site-packages/pyzmq-2.2.0-py2.7-macosx-10.7-intel.egg/zmq/eventloop/ioloop.py", line 434, in _run_callback 
    callback() 
  File "/Library/Python/2.7/site-packages/tornado/iostream.py", line 304, in wrapper 
    callback(*args) 
  File "/Library/Python/2.7/site-packages/tornado/httpserver.py", line 262, in _on_headers 
    self.request_callback(self._request) 
  File "/Library/Python/2.7/site-packages/tornado/web.py", line 1412, in __call__ 
    handler._execute(transforms, *args, **kwargs) 
  File "/Library/Python/2.7/site-packages/tornado/web.py", line 1025, in _execute 
    self._handle_request_exception(e) 
  File "/Library/Python/2.7/site-packages/tornado/web.py", line 1065, in _handle_request_exception 
    self.send_error(500, exc_info=sys.exc_info()) 
  File "/Library/Python/2.7/site-packages/tornado/web.py", line 720, in send_error 
    self.finish() 
  File "/Library/Python/2.7/site-packages/tornado/web.py", line 700, in finish 
    self.flush(include_footers=True) 
  File "/Library/Python/2.7/site-packages/tornado/web.py", line 660, in flush 
    self.request.write(headers + chunk, callback=callback) 
  File "/Library/Python/2.7/site-packages/tornado/httpserver.py", line 429, in write 
    self.connection.write(chunk, callback=callback) 
  File "/Library/Python/2.7/site-packages/tornado/httpserver.py", line 177, in write 
    assert self._request, "Request closed" 
AssertionError: Request closed 

注意:这似乎是与 pyzmq 相关的问题,因为在排除 pyzmq ioloop 后消失。

服务器没有挂掉,可以被其他客户端使用,所以问题不大。不过,在日志文件中发现这些巨大的令人困惑的痕迹非常烦人。

那么,有什么众所周知的方法可以解决这个问题吗? 谢谢。

请您参考如下方法:

这不是 ZMQ 问题。除超时原因外,请求可以关闭。 ZMQ 唯一的问题是它们引发了 AssertionError,这是常见的,而不是更具体的异常。

如果您确定不希望在日志文件中出现这些异常 - 执行如下操作:

try:  
    time_consuming_task() 
except AssertionError as e: 
    if e.message == 'Request closed':  
        logging.info('Bad, annoying client, came to us again!')  
    else: 
        raise e