I have the following code:
def application(env, start_response):
path = process(env)
fh = open(path,'r')
start_response('200 OK', [('Content-Type','application/octet-stream')])
return fbuffer(fh,10000)
def fbuffer(f, chunk_size):
'''Generator to buffer file chunks'''
while True:
chunk = f.read(chunk_size)
if not chunk: break
yield chunk
I'm not sure that it's right but the scraps of information I've found on the internet have led me to think it ought to work. Basically I want to stream a file out in chunks, and to do that I'm passing a generator back from my application function. However this only prints out the headers and doesn't actually send back any data, can anyone tell me why this is?
Alternatively, if this is completely wrong, what's the best way to do this? I can't buffer the whole file in memory as the files I'll be working with are potentially gigabytes large.
Tertiary question: What's the best way to close the file once I'm done outputting it? In the code I posted I can't see anyway to actually close the file.
(I'm running python 3.2.3 with uWSGI 1.2.4)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…