2012-07-31 5 views
3

Django 용 ajax 파일 업 로더를 만들었지 만 업로드 된 각 파일은 개 (30-80MB)의 메모리를 차지하며, 그걸 풀어주는 것처럼 보입니다.파일 업 로더 (Django on Heroku)의 Ajax 구현시 메모리 누수

우리는 dyno 당 512MB의 메모리를 할당하는 Heroku를 사용 중이므로 빨리 메모리 초과 오류가 발생하기 시작합니다. 여기

는 요청을 처리 할 수있는 장고보기 코드 :

if request.is_ajax(): 
     # the file is stored raw in the request 
     upload = request 
     is_raw = True 
     try: 
      filename = request.GET[ 'add_image' ] 
     except KeyError: 
      return HttpResponseBadRequest("AJAX request not valid") 
     (fileBaseName, fileExtension)=os.path.splitext(filename) 

     uniquename = biz_id + "__" + get_a_uuid() + fileExtension 
     saved = save_upload(upload, uniquename, biz) 

을 그리고 여기 save_upload 코드 :이 코드는 알렉스 KUHL와 천둥 토끼에 this post (감사에서 적응

try: 
    #BusinessImage is my Django model. It uses django-imagekit to processs 
    #the raw uploaded image into three sizes (plus the original) 
    bi = BusinessImage(name=uploaded.GET.get("name"), business=biz) 
    if not BusinessImage.objects.filter(business=biz).exists(): 
     bi.primary_image = True 
    bi.original_image.save(filename,ContentFile(uploaded.read())) 
except IOError: 
    # could not open the file most likely 
    return False 
finally: 
    uploaded.close() 
return True 

).

메모리 문제가 django-imagekit과 관련이 있거나 파일을 제대로 닫지 않았다고 생각합니다. 그러나 확실하지 않습니다. 나는 정말 어떤 도움을 주셔서 감사합니다.

감사합니다.

클레이

+1

저는 이것이 django-imagekit과 직접 관련이 있다고 생각하지 않습니다. 같은 js 코드를 기반으로하는 django-ajax-uploader에도 누수가 존재합니다. 참조 : https://github.com/GoodCloud/django-ajax-uploader/issues/12. –

+0

@DirkEschler - 정말 감사합니다. Dirk. django-imagekit은 메모리 문제를 문서화하고 있지만 (https://github.com/jdriscoll/django-imagekit/issues/63 참조), 멀리 떨어진 곳으로 마이그레이션 한 후에도 문제가 있음을 발견했습니다. 귀하의 링크가 그것들을 설명합니다. 다시 한번 감사드립니다. –

답변

1

장고 핸들러 처리 및 trasforming 파일을 일반적으로하지 않습니다 업로드, 그들은 (/ tmp로 저장하거나 메모리에 저장 통해) request.FILES 배열의 전망에 전달하기가 준비를위한 것입니다.

쉽고 빠르며 낮은 메모리 방식으로 진행 피드백을 받으려면 [nginx upload module][1] (및 업로드 진행률)을 사용해보세요. 업로드를 디스크의 지정된 위치에 복사하고 POST 경로에 파일 경로, 크기 및 MIME 유형이 포함 된보기로 요청을 전달합니다. django를 사용하는 것보다 훨씬 효율적입니다.