执行“request.POST.copy()”时出现内存错误
Memory Error while doing `request.POST.copy()'
我有一个允许用户上传文件的 Django 应用程序。
我使用 tastypie
rest 来做到这一点,它可以完美地处理普通大小的文件。
但是上传500MB的文件时出现错误。
错误发生在:
multipart_data = request.POST.copy()
错误是:
Traceback (most recent call last):
File "<env_path>\lib\site-packages\tastypie\resources.py", line 227, in wrapper
response = callback(request, *args, **kwargs)
File "<env_path>\lib\site-packages\tastypie\resources.py", line 467, in dispatch_list
return self.dispatch('list', request, **kwargs)
File "<env_path>\lib\site-packages\tastypie\resources.py", line 499, in dispatch
response = method(request, **kwargs)
File "<env_path>\lib\site-packages\tastypie\resources.py", line 1405, in post_list
deserialized = self.deserialize(request, request.body, format=request.META.get('CONTENT_TYPE', 'application/json'))
File "<project_path>\apps\data_manager\rest.py", line 70, in deserialize
multipart_data = request.POST.copy()
File "<env_path>\lib\site-packages\django\core\handlers\wsgi.py", line 110, in _get_post
self._load_post_and_files()
File "<env_path>\lib\site-packages\django\http\request.py", line 315, in _load_post_and_files
self._post, self._files = self.parse_file_upload(self.META, data)
File "<env_path>\lib\site-packages\django\http\request.py", line 275, in parse_file_upload
return parser.parse()
File "<env_path>\lib\site-packages\django\http\multipartparser.py", line 254, in parse
chunk = handler.receive_data_chunk(chunk, counters[i])
File "<env_path>\lib\site-packages\django\core\files\uploadhandler.py", line 174, in receive_data_chunk
self.file.write(raw_data)
MemoryError
根据 documentation,MemoryFileUploadHandler 不适用于大文件。
所以下面的解决方案是有效的,在 settings.py
:
FILE_UPLOAD_HANDLERS = [
'django.core.files.uploadhandler.TemporaryFileUploadHandler',
]
我有一个允许用户上传文件的 Django 应用程序。
我使用 tastypie
rest 来做到这一点,它可以完美地处理普通大小的文件。
但是上传500MB的文件时出现错误。
错误发生在:
multipart_data = request.POST.copy()
错误是:
Traceback (most recent call last):
File "<env_path>\lib\site-packages\tastypie\resources.py", line 227, in wrapper
response = callback(request, *args, **kwargs)
File "<env_path>\lib\site-packages\tastypie\resources.py", line 467, in dispatch_list
return self.dispatch('list', request, **kwargs)
File "<env_path>\lib\site-packages\tastypie\resources.py", line 499, in dispatch
response = method(request, **kwargs)
File "<env_path>\lib\site-packages\tastypie\resources.py", line 1405, in post_list
deserialized = self.deserialize(request, request.body, format=request.META.get('CONTENT_TYPE', 'application/json'))
File "<project_path>\apps\data_manager\rest.py", line 70, in deserialize
multipart_data = request.POST.copy()
File "<env_path>\lib\site-packages\django\core\handlers\wsgi.py", line 110, in _get_post
self._load_post_and_files()
File "<env_path>\lib\site-packages\django\http\request.py", line 315, in _load_post_and_files
self._post, self._files = self.parse_file_upload(self.META, data)
File "<env_path>\lib\site-packages\django\http\request.py", line 275, in parse_file_upload
return parser.parse()
File "<env_path>\lib\site-packages\django\http\multipartparser.py", line 254, in parse
chunk = handler.receive_data_chunk(chunk, counters[i])
File "<env_path>\lib\site-packages\django\core\files\uploadhandler.py", line 174, in receive_data_chunk
self.file.write(raw_data)
MemoryError
根据 documentation,MemoryFileUploadHandler 不适用于大文件。
所以下面的解决方案是有效的,在 settings.py
:
FILE_UPLOAD_HANDLERS = [
'django.core.files.uploadhandler.TemporaryFileUploadHandler',
]