Harish
Harish

Reputation: 425

python django exporing excel server down

My website has a feature of exporting daily report in excel which may vary according to users. Due to some reason i can't consider redis or memcache. For each user the no of rows in db are around 2-5 lacks. when user call the export-to-excel feature it takes 5-10 minutes to export and till that website all resources(ram,cpu) are used in making that excel and that results site-down for 5 minutes and after 5 minutes everything work fine. I also chunked the the query result in small part for solving RAM issue it solves my 50 percent problem. is there is any other solution for CPU and RAM optimization?

sample code

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

Upvotes: 2

Views: 98

Answers (1)

kapilsdv
kapilsdv

Reputation: 719

Streaming a file that takes a long time to generate you can avoid a load balancer dropping a connection that might have otherwise timed out while the server was generating the response.

  • You can also process your request asynchronously using celery.

Processing requests asynchronously will allow your server to accept any other request while the previous one is being processed by the worker in the background.

Thus your system will become more user friendly in that manner.

Upvotes: 1

Related Questions