Django文件上传偶尔会失败

时间:2009-01-04 23:28:35

标签: python django apache

我正在尝试将我的第一个Django 1.0.2应用程序移植到OSX / Leopard上,使用Apache + mod_python 3.3.1 + python 2.6.1(全部以64位模式运行)并且我偶尔会遇到错误上传使用Django开发服务器测试时不存在的文件。

上传代码类似于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})

此代码通常可以正常工作,但有时(例如,每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?)

我尝试使用mod_wsgi运行所有内容并没有区别。

有人知道我做错了什么吗?

提前感谢您的帮助!

PPDO

=====

更新:

虽然我成功上传了大文件(60+ MB),但是当它失败时它失败并且与上传的大小没有明显的关系,即它也失败了以前成功上传的10kB文件。

4 个答案:

答案 0 :(得分:8)

使用mod_wsgi让Firefox的问题消失了。

将我的研究限制在Apache和Safari之间的交互问题上,我偶然发现了Apache https://bugs.webkit.org/show_bug.cgi?id=5760的这个错误报告,该报告描述了与正在发生的事情非常相似的事情,并且显然仍然是开放的。读这个给了我尝试和禁用keepalive的想法,虽然我需要更广泛地测试它,但似乎问题已经消失。

简单:

BrowserMatch“Safari”nokeepalive

Apache配置中的

就行了。

答案 1 :(得分:1)

我会追逐异常值

Client read error (Timeout?)
这似乎很奇怪。请尝试阅读this thread

答案 2 :(得分:0)

文件有多大?上传脚本超时可能需要很长时间,因此请尝试增加该脚本的执行时间。

答案 3 :(得分:0)

这里很长时间没有回答,但我自己经历了这个并尝试了一些事情来解决它。在我的情况下,如果有人在上传过程中取消了下载(或丢失了连接),就会发生这种情况。

您可以自己尝试一下,看看这是否是导致“IOError:请求数据读取错误”的原因:上传足够大的文件,您可以从计算机或路由器上拔下以太网电缆。稍等一下,看看是否收到错误。我每次都这样做!