4

I have existing code that serializes data to a file-like object:

def some_serialization_function(file):
    file.write(...)

In Flask, I would like to be able to send the serialized data directly to the client, without buffering it in memory first.

I looked at ResponseStreamMixin from werkzeug, but I don't think it can work without buffering:

class StreamResponse(flask.Response, werkzeug.wrappers.ResponseStreamMixin):
   pass

@app.route("/data")
def get_data():
   r = StreamResponse()
   some_serialization_function(r.stream) # everything is buffered into memory
   return r # buffered data is sent after return

All examples for streaming data that I found are based on generators, which work in the opposite direction (ie data is "pulled" from the generator, not "pushed out" via a write call), so I wonder, is there a way to "write" directly to the client in Flask?

EDIT - to be more clear: I'm looking for a way to serve the data generated by "some_serialization_function(...)" (which I cannot easily change) without the memory/IO overhead of having that function write all the data to a buffer/file first.

(I suspect that a tempfile will be the way to go in the end, since the IO overhead will not be significant in comparison to the overhead of actually sending the data over the network. Also my main concern is memory overhead).

2 Answers 2

5

You can create a special file-like object that feeds the generator that streams out to the client. Here is a quick & dirty implementation using a queue:

from queue import Queue

class StreamWriter(object):
    def __init__(self):
        self.queue = Queue()

    def write(self, str):
        self.queue.put(str)

    def read(self):
        str = self.queue.get()
        self.queue.task_done()
        if str == '~':
            return None
        return str

    def close(self):
        self.write('~')  # indicate EOF

This is nothing more than a pub-sub type queue. The read() method will block until there is something written in another thread.

Now you can stream a response using a generator. The following example shows a generator that takes the serialization function as an argument. The serialization function is executed in a background thread and receives the file-like object as an argument.

def generate_response(serialize):
    file = StreamWriter()
    def serialize_task():
        serialize(file)
        file.close()
    threading.Thread(target=serialize_task).start()
    while True:
        chunk = file.read()
        if chunk is None:
            break
        yield chunk

I hope this helps!

2
  • This is helpful, but an actual implementation would need to be more involved i think (communicating errors/exceptions from the thread comes to mind). The pragmatic thing to do in my use case will be to use a tempfile.
    – hmn
    May 7, 2014 at 16:35
  • Yes, this is a simple test to demonstrate how this can be done. It is not robust enough for production use. May 7, 2014 at 20:19
0

If I understood you well, you want

  • Flask web app providing stream of data
  • Client get the data piece by piece, not in one large chunk
  • Flask web app being in control, thus initiating the write app.

I think, this cannot be done as someone must be in control of the flow and in case of web app it is client, who is reading the data.

On the other hand, if you want prevent buffering whole content to be provided to the client on web app, you can read the data on web server piece by piece and yield it part by part.

Providing content by server piece by piece

from flask import Flask, Response, request
app = Flask(__name__)

@app.route('/')
def hello_world():
    return 'Hello World!'

@app.route('/loop')
def loop():
    def generate():
        yield "Hello"
        yield "World"
    return Response(generate())

@app.route('/longloop/<int:rows>')
def longloop(rows):
    def generate(rows):
        for i in xrange(rows):
            yield "{i}: Hello World".format(i=i)
    return Response(generate(rows))

if __name__ == '__main__':
    app.run(debug=True)

The trick is to use Response object with generator generating output.

If you visit http://localhost:5000/longloop/100, you shall receive 100 greeting.

Try this from command line using curl and better redirect out put to /dev/null:

$ curl -X GET http://localhost:5000/longloop/120000000 > /dev/null                                                                                                                              
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  538M    0  538M    0     0  1056k      0 --:--:--  0:08:41 --:--:-- 1079k

As we can see, the script runs more than 8 minutes now and memory consumed by flask app is still the same about the same, in my case it keeps at 0.4% of total RAM.

4
  • "Streaming" may be misleading, what I want is to serve the data created by that function without the memory overhead of buffering.
    – hmn
    May 6, 2014 at 1:51
  • @hmn Example of Flask app provided in my edited answer. May 6, 2014 at 6:45
  • Sorry, that does not help me. I already knew how to stream a response using generators. My problem is that the function generating the data (which I cannot change easily) works in the opposite direction.
    – hmn
    May 6, 2014 at 8:25
  • @hmn If your serialization function writes at once all the data to some file, you have to buffer it somewhere, there is no other choice. You may use temporary file or StringIO buffer for that. If you want to prevent this intermediate buffering, you really have to change your serialization function into some iterator or generator. May 6, 2014 at 8:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.