How to transfer packet length from upload method to streaming content?

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

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

The function generatefrom the example uploads files to an FTP server, as described in the ftputil documentation .

The callback function [ callback(chunk)] used in the method uploadis executed for each loaded fragment of the file.

Is it possible to output len(chunk)from a callback to a stream? Any dirty hacks are also very welcome.

Thanks for any help!

+3
source share
1 answer

, ftp.upload() , . , , , , .

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())
+1

All Articles