當我嘗試按照pyzmq official documentation中所述組合Tornado和pyzmq ioloops時,遇到了一個惱人的問題(雖然不是關鍵)。結合龍捲風和zmq ioloops:通過同位異常重置連接
我有接受來自客戶端(C)REST API請求,並代理他們運行的進程龍捲風(T)服務器雖然ZMQ運輸到另一個進程(Z),做實際工作。
C <-> T <-> Z
如果C關閉連接之前Ž答覆T,Z(龍捲風)輸出一束長異常跡線(見在底部)。想象一下下面的例子:
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同行,它只是重視pyzmq ioloop龍捲風的ioloop。雖然,這足以說明問題。
從控制檯一個運行服務器:
% python example.py
從控制檯兩個運行客戶端和(在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
NOTE:看來它是pyzmq rel因爲排除pyzmq ioloop後會消失。
服務器不死,它可以被其他客戶端使用,所以問題並不重要。儘管如此,在日誌文件中發現這些巨大的混淆痕跡非常煩人。
那麼,有沒有什麼衆所周知的方法來解決這個問題? 謝謝。