龙卷风:如果 exception/error 在 data_received 中将分块数据流式传输到文件时如何关闭文件对象

Tornado: how to close file object if exception/error in data_received when streaming chunked data to file

我是 运行 Tornado,stream_request_body 并保存文件 POST 请求到服务器上的文件。在 prepare 中,我创建了文件对象,在 post 中,我创建了 close 文件对象。我如何捕获 data_received 中的任何 error/exception 以便我可以正确 close 文件对象?

from tornado.ioloop import IOLoop
from tornado.web import Application, RequestHandler, stream_request_body

@stream_request_body
class UploadHandler(RequestHandler):

    def prepare(self):
        self.file = open('uploaded_file', 'wb')

    def data_received(self, chunk):
        print(len(chunk))
        self.file.write(chunk)

        # force an exception
        raise Exception('Error')

    def post(self, *args, **kwargs):
        self.file.close()
        self.write('UPLOADED')


app = Application([(r'/upload', UploadHandler), ],
                  debug=True)


if __name__ == '__main__':
    app.listen(7777)
    IOLoop.current().start()

Tornado 应用程序的终端输出 运行

$ python upload.py
232
ERROR:tornado.application:Uncaught exception
Traceback (most recent call last):
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib64/python3.6/site-packages/tornado/http1connection.py", line 659, in _read_fixed_body
    ret = delegate.data_received(body)
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib64/python3.6/site-packages/tornado/routing.py", line 264, in data_received
    return self.delegate.data_received(chunk)
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib64/python3.6/site-packages/tornado/web.py", line 2279, in data_received
    return self.handler.data_received(data)
  File "upload.py", line 13, in data_received
    raise Exception('Error')
Exception: Error

带有python请求解释器的终端输出

>>> resp=requests.post('http://localhost:7777/upload', files={'file': open('test.txt', 'rb')})
Traceback (most recent call last):
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 706, in urlopen
    chunked=chunked,
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 445, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 440, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib64/python3.6/http/client.py", line 1346, in getresponse
    response.begin()
  File "/usr/lib64/python3.6/http/client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "/usr/lib64/python3.6/http/client.py", line 276, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
http.client.RemoteDisconnected: Remote end closed connection without response

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/adapters.py", line 449, in send
    timeout=timeout
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 756, in urlopen
    method, url, error=e, _pool=self, _stacktrace=sys.exc_info()[2]
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/util/retry.py", line 532, in increment
    raise six.reraise(type(error), error, _stacktrace)
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/packages/six.py", line 734, in reraise
    raise value.with_traceback(tb)
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 706, in urlopen
    chunked=chunked,
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 445, in _make_request
    six.raise_from(e, None)
  File "<string>", line 3, in raise_from
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/urllib3/connectionpool.py", line 440, in _make_request
    httplib_response = conn.getresponse()
  File "/usr/lib64/python3.6/http/client.py", line 1346, in getresponse
    response.begin()
  File "/usr/lib64/python3.6/http/client.py", line 307, in begin
    version, status, reason = self._read_status()
  File "/usr/lib64/python3.6/http/client.py", line 276, in _read_status
    raise RemoteDisconnected("Remote end closed connection without"
urllib3.exceptions.ProtocolError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/api.py", line 119, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/api.py", line 61, in request
    return session.request(method=method, url=url, **kwargs)
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/sessions.py", line 542, in request
    resp = self.send(prep, **send_kwargs)
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/sessions.py", line 655, in send
    r = adapter.send(request, **kwargs)
  File "/prog/res/komodo/2021.08.03-py36-rhel7/root/lib/python3.6/site-packages/requests/adapters.py", line 498, in send
    raise ConnectionError(err, request=request)
requests.exceptions.ConnectionError: ('Connection aborted.', RemoteDisconnected('Remote end closed connection without response',))

RequestHandler 有一个 on_connection_close method ,它在连接关闭时被调用。您可以覆盖它以关闭文件对象:

class UploadHandler(...):
    ...
    def on_connection_close(self):
        self.file.close()