2013-02-24 56 views
0

我製作了一個簡單的文件服務器,運行在我的樹莓派(1/2 GB內存,1個CPU)上。它運行在nginx(1名工作人員)背後的gunicorn(3名工作人員)之下。Django:片狀文件下載

我有一個奇怪的問題,當我試圖同時下載太多的文件(比如5)時,他們都會部分通過,然後放棄。 django服務器沒有輸出(我也使用開發服務器來解決這個問題,這就是爲什麼它現在運行在nginx的後面,但仍然沒有喜悅)。

我下載的觀點是:

@never_cache 
def download_media(request, user_id, session_key, id, filepath): 
    "Download an individual media file" 

    context = RequestContext(request) 

    # validate the user_id & session_key pair 
    if not __validate_session_key(user_id, session_key): 
     return HttpResponseRedirect(reverse('handle_logout')) 

    filepath = unicode(urllib.unquote(filepath)) 

    if '..' in filepath: 
     raise SuspiciousOperation('Invalid characters in subdir parameter.') 

    location = MediaCollectionLocation.objects.get(id=id) 

    path = os.path.join(location.path, filepath) 

    response = HttpResponse(FileWrapper(file(path)), content_type='application/octet-stream') 
    response['Content-Disposition'] = 'attachment; filename=%s' % os.path.basename(path) 

    response['Content-Length'] = os.path.getsize(path) 
    response["Cache-Control"] = "no-cache, no-store, must-revalidate" 
    return response 

我提供文件服務這種方式,因爲我希望客戶端進行身份驗證(所以不要只是想重定向和服務nginx的靜態內容)。

任何人都知道爲什麼它會退出,如果我並行發出幾個請求?

+0

你有沒有試過在另一臺linux機器上運行這個腳本(即不是在你的樹莓派 - 大概運行raspbian)? – Hzmy 2013-02-24 23:04:10

回答

0

我不完全確定爲什麼這些文件會失敗,但我想可能與下載更多的文件有關,比您有工作人員,或者在nginx和gunicorn之間發生超時。

只有在django通過讓django設置一個特定的頭文件,然後nginx讀取(僅限內部文件)併爲文件提供服務之後,才能讓nginx提供文件。

XSendFile是nginx用來做到這一點的。然後,您可以創建一些中間件或函數來設置django中的相應頭文件,或者使用類似django-sendfile的內容與nginx後端一起完成。

如果您遇到的問題是由django和nginx之間的超時引起的,則此修復程序應解決此問題。如果不是,則也增加nginx工作者的數量,因爲它現在負責提供文件。

+0

不知道。好多了,謝謝。 – 2013-02-25 19:29:23