假设,(使用龙卷风)有人想将大数据传输到慢速客户端,并打印传输的数据量。为此,我编写了这个简单的代码:
from tornado import web, ioloop, gen
class MainHandler(web.RequestHandler):
@web.asynchronous
@gen.engine
def get(self):
print '( start'
try:
for i in xrange(100):
self.write('*' * 100000) # write ~100KB of data
yield gen.Task(self.flush) # wait for reciever to recieve
print '- wrote', i
finally:
print '| finally'
self.finish()
print ') finish'
application = web.Application([
(r"/", MainHandler),
])
application.listen(8888)
ioloop.IOLoop.instance().start()
但是这段代码没有按预期工作,例如使用这个简单的客户端:
req = urlopen('http://127.0.0.1:8888/')
sys.exit() # exit without reading response
服务器输出:
( start
- wrote 0
- wrote 1
- wrote 2
WARNING:root:Read error on 7: [Errno 104] Connection reset by peer
WARNING:root:error on read
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/tornado-2.4.1-py2.7.egg/tornado/iostream.py", line 361, in _handle_read
if self._read_to_buffer() == 0:
File "/usr/local/lib/python2.7/dist-packages/tornado-2.4.1-py2.7.egg/tornado/iostream.py", line 428, in _read_to_buffer
chunk = self._read_from_socket()
File "/usr/local/lib/python2.7/dist-packages/tornado-2.4.1-py2.7.egg/tornado/iostream.py", line 409, in _read_from_socket
chunk = self.socket.recv(self.read_chunk_size)
error: [Errno 104] Connection reset by peer
但既不finally
也不finish
打印。
有什么问题?!
更新:
我注意到如果我实现on_connection_close
处理程序,on_connection_close
回调将被调用,并且控制台中不会打印任何错误。但是这个功能有什么魔力呢?(我已经RequestHandler
用空体定义了)。
更大的答案:为什么我的发电机不是close
d?是垃圾收集还是不收集?PEP-342 说 finally 块应该在任何情况下执行。