2016-07-20 97 views
2

我的網站有一個出口excel日報的功能,可能因用戶而異。由於某些原因,我不能考慮redis或memcache。對於每個用戶,db中的行數都在2-5左右。當用戶調用export-to-excel功能時,需要5-10分鐘才能導出,並且直到該網站使用所有資源(ram,cpu)來製作該excel,並且結果網站停工5分鐘,5分鐘後一切正常精細。我也分解了查詢結果的一小部分來解決RAM問題,它解決了我的50%問題。有沒有其他的CPU和RAM優化解決方案?python django excel server down down

示例代碼

def import_to_excel(request): 
    order_list = Name.objects.all() 
    book = xlwt.Workbook(encoding='utf8') 
    default_style = xlwt.Style.default_style 
    style = default_style 
    fname = 'order_data' 
    sheet = book.add_sheet(fname) 
    row = -1 
    for order in order_list: 
     row+=1 
     sheet.write(row, 1,order.first_name, style=style) 
     sheet.write(row, 2,order.last_name, style=style) 
    response = HttpResponse(mimetype='application/vnd.ms-excel') 
    response['Content-Disposition'] = 'attachment; filename=order_data_pull.xls' 
    book.save(response) 
    return response 
+1

不是關於內存或CPU優化(但它可能會顯示一些差異)。您可以嘗試使用[StreamingHttpResponse](https://docs.djangoproject.com/en/1.9/howto/outputting-csv/#streaming-large -csv-files),並且還可以[celery]異步處理你的請求(http://docs.celeryproject.org/en/latest/django/first-steps-with-django.html)。將需要很長時間生成時間可以避免負載均衡器丟棄連接,其次異步會在後臺處理您的請求,並且您的服務器可以自由處理另一個請求,從而使其更友好。 – kapilsdv

+0

感謝@KapilSachdev它的工作原理 – Harish

+0

[如果你想說「謝謝你」](http://stackoverflow.com/help/someone-answers),接受和upvote :-)。 – kapilsdv

回答

1

流的文件需要很長的時間來產生可避免負載平衡器下降可能的連接否則在服務器生成響應時超時。

  • 您也可以使用異步處理celery您的要求。

異步處理請求將允許服務器接受任何其他請求,而前一個請求正由後臺的工作人員處理。

因此,您的系統將以這種方式變得更加用戶友好。