3

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!

VisioN
  • 143,310
  • 32
  • 282
  • 281

1 Answers1

1

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())
jd.
  • 10,678
  • 3
  • 46
  • 55
  • That's a nice issue. I was trying to make something with threads and queues but it failed to work. I will check it. Thank you! – VisioN May 16 '12 at 09:44