当我调用self.finish()来结束异步请求时,我正在使用龙卷风2.0,我会得到一条带有“Stream is closed”消息的IOError。当客户端在服务器调用finish()之前结束请求(即通过导航到另一个页面)时,看起来好像会发生这种情况。这是预期的行为和我的代码需要处理的东西吗?我发现这个错误来自一年前,这表明这不是客户端代码应该处理的问题:https://github.com/facebook/tornado/issues/81。这是否表示我的代码中存在错误,如果是,可能的原因是什么?
堆栈跟踪:
Traceback (most recent call last):
File "my_code.py", line 260, in my_method
self.finish()
File "/usr/lib/python2.6/site-packages/tornado/web.py", line 634, in finish
self.request.finish()
File "/usr/lib/python2.6/site-packages/tornado/httpserver.py", line 555, in finish
self.connection.finish()
File "/usr/lib/python2.6/site-packages/tornado/httpserver.py", line 349, in finish
self._finish_request()
File "/usr/lib/python2.6/site-packages/tornado/httpserver.py", line 372, in _finish_request
self.stream.read_until(b("\r\n\r\n"), self._header_callback)
File "/usr/lib/python2.6/site-packages/tornado/iostream.py", line 137, in read_until
self._check_closed()
File "/usr/lib/python2.6/site-packages/tornado/iostream.py", line 403, in _check_closed
raise IOError("Stream is closed")
IOError: Stream is closed
答案 0 :(得分:0)
调用self.finish()来结束异步请求,而像self.render()这样的函数会调用self.finish()。
如果在关闭连接后调用self.finish(),则会导致错误。
所以你可以在self.finish()
之前检查你是否调用了一些完成连接的函数或者你可以这样做:
if not self._finished:
#if the connection is closed, it won't call this function
self.finish()
else:
pass