gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 06:35:45 27 4
gpt4 key购买 nike

当我尝试按照 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 1Ok, time to replyWARNING:root:Read error on 24: [Errno 54] Connection reset by peerERROR: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 peerERROR:root:Cannot send error response after headers writtenERROR: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 closedERROR: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

关于python - 结合 Tornado 和 zmq ioloops : Connection reset by peer exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11467615/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com