Med*_*her 4 python wsgi large-files
我有以下代码:
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
Run Code Online (Sandbox Code Playgroud)
我不确定它是对的,但是我在互联网上找到的信息碎片让我觉得它应该有效.基本上我想以块的形式流出一个文件,为此,我从我的应用程序函数传回一个生成器.然而,这只打印出标题,并没有实际发回任何数据,任何人都可以告诉我为什么这是?
或者,如果这是完全错误的,那么最好的方法是什么?我无法将整个文件缓冲在内存中,因为我将使用的文件可能是千兆字节.
第三个问题:一旦我输出文件,关闭文件的最佳方法是什么?在我发布的代码中,无论如何我都看不到实际关闭文件.
(我用uWSGI 1.2.4运行python 3.2.3)
没有一点小心,uwsgi小心不要让错误泄漏,但如果你在更严格的实现中运行你的应用程序,比如python提供的那个wsgiref.simple_server,你可以更容易地看到问题.
Serving <function application at 0xb65848> http://0.0.0.0:8000
Traceback (most recent call last):
File "/usr/lib64/python3.2/wsgiref/handlers.py", line 138, in run
self.finish_response()
File "/usr/lib64/python3.2/wsgiref/handlers.py", line 179, in finish_response
self.write(data)
File "/usr/lib64/python3.2/wsgiref/handlers.py", line 264, in write
"write() argument must be a bytes instance"
AssertionError: write() argument must be a bytes instance
localhost.localdomain - - [04/Aug/2012 16:27:08] "GET / HTTP/1.1" 500 59
Run Code Online (Sandbox Code Playgroud)
问题是wsgi要求传输到HTTP网关和从HTTP网关传输的数据必须作为服务bytes,但是当你使用时open(path, 'r'),python 3 str使用默认编码方便地将读取的数据转换为unicode,即python 3中的数据.
改变
fh = open(path, 'r')
Run Code Online (Sandbox Code Playgroud)
至
fh = open(path, 'rb')
# ^
Run Code Online (Sandbox Code Playgroud)
解决它.
| 归档时间: |
|
| 查看次数: |
3450 次 |
| 最近记录: |