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
不是關於內存或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
感謝@KapilSachdev它的工作原理 – Harish
[如果你想說「謝謝你」](http://stackoverflow.com/help/someone-answers),接受和upvote :-)。 – kapilsdv