How to pass chunk length from upload method to streaming content?

530 Views Asked by At

In my Flask project I use ftputil library. In one of the application sections I use content streaming as described in Flask documentation:

@app.route('/section')
def section():
    def generate():
        ftp.upload(source, target, "b", callback)
        yield 'completed'
    return Response(generate())

Function generate from the example makes file uploading to FTP server as described in ftputil documentation.

Callback function [callback(chunk)] used in upload method executes for each uploaded file chunk.

Is there any posibility to output len(chunk) from the callback to the stream? Any dirty hacks are also very much welcome.

Thanks for any help!

1

There are 1 best solutions below

1
jd. On

I'm assuming that ftp.upload() runs synchronously, which would make sense. I haven't tested the code below so it's probably riddled with bugs but the idea should work.

import threading, Queue

@app.route('/section')
def section():
    q = Queue.Queue()
    def callback(chunk):
        q.put(len(chunk))
    t = threading.Thread(target=lambda: ftp.upload(source, target, "b", callback) or q.put(None))
    t.setDaemon(True)
    t.start()
    def generate():
        while 1:
            l = q.get()
            if l is None:
                return
            yield unicode(l) + '\n'
    return Response(generate())