apache Django文件上传偶尔失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/411902/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django file upload failing occasionally
提问by
I am trying to port my first Django 1.0.2 application to run on OSX/Leopard with Apache + mod_python 3.3.1 + python 2.6.1 (all running in 64-bit mode) and I am experiencing an occasional error when uploading a file that was not present when testing with the Django development server.
我正在尝试将我的第一个 Django 1.0.2 应用程序移植到 OSX/Leopard 上运行 Apache + mod_python 3.3.1 + python 2.6.1(均以 64 位模式运行),并且在上传文件时偶尔会遇到错误在使用 Django 开发服务器进行测试时不存在。
The code for the upload is similar to what described in the Django documentation:
上传的代码类似于 Django 文档中的描述:
class UploadFileForm(forms.Form):
file = forms.FileField()
description = forms.CharField(max_length=100)
notifygroup = forms.BooleanField(label='Notify Group?', required=False)
def upload_file(request, date, meetingid ):
print date, meetingid
if request.method == 'POST':
print 'before reloading the form...'
form = UploadFileForm(request.POST, request.FILES)
print 'after reloading the form'
if form.is_valid():
try:
handle_uploaded_file(request.FILES['file'], request.REQUEST['date'], request.REQUEST['description'], form.cleaned_data['notifygroup'], meetingid )
except:
return render_to_response('uploaded.html', { 'message': 'Error! File not uploaded!' })
return HttpResponseRedirect('/myapp/uploaded/')
else:
form = UploadFileForm()
return render_to_response('upload.html', {'form': form, 'date':date, 'meetingid':meetingid})
This code normally works correctly, but sometimes (say, once every 10 uploads) and after a fairly long waiting time, it fails with the following error:
此代码通常可以正常工作,但有时(例如,每 10 次上传一次)并且经过相当长的等待时间后,它会失败并显示以下错误:
IOError at /myapp/upload/2009-01-03/1
Client read error (Timeout?)
Request Method: POST
Request URL: http://192.168.0.164/myapp/upload/2009-01-03/1
Exception Type: IOError
Exception Value:
Client read error (Timeout?)
Exception Location: /Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py in read, line 406
Python Executable: /usr/sbin/httpd
Python Version: 2.6.1
Python Path: ['/djangoapps/myapp/', '/djangoapps/', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python26.zip', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/plat-darwin', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/plat-mac', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/plat-mac/lib-scriptpackages', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/lib-tk', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/lib-old', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/lib-dynload', '/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages']
Server time: Sun, 4 Jan 2009 22:42:04 +0100
Environment:
Request Method: POST
Request URL: http://192.168.0.164/myapp/upload/2009-01-03/1
Django Version: 1.0.2 final
Python Version: 2.6.1
Installed Applications:
['django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.sites',
'django.contrib.admin',
'myapp.application1']
Installed Middleware:
('django.middleware.common.CommonMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware')
Traceback:
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/core/handlers/base.py" in get_response
86. response = callback(request, *callback_args, **callback_kwargs)
File "/djangoapps/myapp/../myapp/application1/views.py" in upload_file
137. form = UploadFileForm(request.POST, request.FILES)
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/core/handlers/modpython.py" in _get_post
113. self._load_post_and_files()
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/core/handlers/modpython.py" in _load_post_and_files
87. self._post, self._files = self.parse_file_upload(self.META, self._req)
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/__init__.py" in parse_file_upload
124. return parser.parse()
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in parse
134. for item_type, meta_data, field_stream in Parser(stream, self._boundary):
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in __iter__
607. for sub_stream in boundarystream:
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in next
421. return LazyStream(BoundaryIter(self._stream, self._boundary))
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in __init__
447. unused_char = self._stream.read(1)
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in read
300. out = ''.join(parts())
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in parts
293. chunk = self.next()
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in next
315. output = self._producer.next()
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in next
376. data = self.flo.read(self.chunk_size)
File "/Library/Frameworks/Python64.framework/Versions/2.6/lib/python2.6/site-packages/django/http/multipartparser.py" in read
406. return self._file.read(num_bytes)
Exception Type: IOError at /myapp/upload/2009-01-03/1
Exception Value: Client read error (Timeout?)
I tried to run everything using mod_wsgi and no difference.
我尝试使用 mod_wsgi 运行所有内容,没有任何区别。
Does anybody know what am I doing wrong?
有谁知道我做错了什么?
Thanks in advance for your help!
在此先感谢您的帮助!
ppdo
点点滴滴
=====
======
Updated:
更新:
Though I succeeded uploading large files (60+ MB), when it fails it fails with no evident relationship with the size of the upload, i.e. it fails also with 10kB files that have successfully been uploaded before.
虽然我成功上传了大文件(60+ MB),但当它失败时它会失败,与上传的大小没有明显关系,即它也会失败,并且之前已经成功上传了 10kB 文件。
回答by
Using mod_wsgi made the problem go away for Firefox.
使用 mod_wsgi 使 Firefox 的问题消失了。
Limiting my research to an interaction problem between Apache and Safari, I stumbled upon this bug report for Apache https://bugs.webkit.org/show_bug.cgi?id=5760that describes something very similar to what is happening and it is apparently still open. Reading this gave me the idea to try and disable the keepalive and, though I need to test it more extensively, it seems the problem is gone.
将我的研究限制在 Apache 和 Safari 之间的交互问题上,我偶然发现了这个关于 Apache https://bugs.webkit.org/show_bug.cgi?id=5760 的错误报告,它描述了与正在发生的事情非常相似的事情,它显然是还开着。阅读这篇文章让我有了尝试禁用 keepalive 的想法,虽然我需要更广泛地测试它,但似乎问题已经解决了。
A simple:
一个简单的:
BrowserMatch "Safari" nokeepalive
BrowserMatch "Safari" nokeepalive
in the Apache configuration did the trick.
在 Apache 配置中做到了这一点。
回答by Bartosz Radaczyński
I would chase down the exception value
我会追赶异常值
Client read error (Timeout?)
this seems odd enough. Try reading this thread
这似乎很奇怪。尝试阅读此线程
回答by Bryan
Long time unanswered here, but having experienced this myself and tried a few things to resolve it. It seems in my case, it happened if someone canceled a download (or lost connection) during an upload.
很长时间没有得到答复,但我自己也经历过这个问题并尝试了一些方法来解决它。在我的情况下,如果有人在上传过程中取消下载(或丢失连接),就会发生这种情况。
You can try this yourself to see if this is what is causing the "IOError: request data read error" for you: upload a large enough file that you can unplug your ethernet cable from your computer or router. Wait a bit and see if you get the error. I do everytime!
您可以自己尝试一下,看看这是否是导致“IOError:请求数据读取错误”的原因:上传足够大的文件,以便您可以从计算机或路由器上拔下以太网电缆。稍等一下,看看是否出现错误。我每次都这样!
回答by Dana the Sane
How large is the file? It may take long enough to upload that the upload script times out, so try increasing the execution time for that script.
文件有多大?由于上传脚本超时,上传可能需要足够长的时间,因此请尝试增加该脚本的执行时间。

