Skip to content

Instantly share code, notes, and snippets.

@baxeico baxeico/views.py
Last active Aug 29, 2015

Embed
What would you like to do?
Find and fix a django "memleak"
import objgraph
import sys
import logging
from django.http import HttpResponse
from .models import FirstModel
logger = logging.getLogger(__name__)
class LoggerWriter:
def __init__(self, logger):
self.logger = logger
def write(self, message):
if message != '\n':
self.logger.debug(message)
def my_view(request):
sys.stdout = LoggerWriter(logger)
huge_queryset = FirstModel.objects.all()
f = open('dumb.dump', 'w')
row = 0
for record in huge_queryset:
print >>f, record
row += 1
if not (row % 1000):
objgraph.show_growth()
f.close()
return HttpResponse('Dumb dump completed!')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.