changeset 3554:c0becca3d0d9

request_wsgi.send_file: use wsgi.file_wrapper (or a builtin simple wrapper). Fixes memory consumption for sending of large file attachments.
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Sun, 04 May 2008 20:56:30 +0200
parents 1052c105b16f
children dc7590f9c3b0
files MoinMoin/request/request_wsgi.py MoinMoin/server/server_wsgi.py
diffstat 2 files changed, 17 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/MoinMoin/request/request_wsgi.py	Sun May 04 18:47:03 2008 +0200
+++ b/MoinMoin/request/request_wsgi.py	Sun May 04 20:56:30 2008 +0200
@@ -24,6 +24,10 @@
             self.status = '200 OK'
             self.headers = []
 
+            # used by send_file()
+            self._send_file = None
+            self._send_bufsize = None
+
             self._setup_vars_from_std_env(env)
             RequestBase.__init__(self, {})
 
@@ -54,6 +58,11 @@
         else:
             return self.stdin.read(n)
 
+    def send_file(self, fileobj, bufsize=8192, do_flush=None):
+        # For now, we just remember fileobj and bufsize for sending it later:
+        self._send_file = fileobj
+        self._send_bufsize = bufsize
+
     def write(self, *data):
         data = self.encode(data)
         try:
--- a/MoinMoin/server/server_wsgi.py	Sun May 04 18:47:03 2008 +0200
+++ b/MoinMoin/server/server_wsgi.py	Sun May 04 20:56:30 2008 +0200
@@ -28,5 +28,12 @@
     request = request_wsgi.Request(environ)
     request.run()
     start_response(request.status, request.headers)
-    return [request.output()]
+    if request._send_file is not None:
+        # moin wants to send a file (e.g. AttachFile.do_get)
+        def simple_wrapper(fileobj, bufsize):
+            return iter(lambda: fileobj.read(bufsize), '')
+        file_wrapper = environ.get('wsgi.file_wrapper', simple_wrapper)
+        return file_wrapper(request._send_file, request._send_bufsize)
+    else:
+        return [request.output()] # don't we have a filelike there also!?