生成器中的内存泄漏

时间:2019-12-18 13:37:06

标签: python-2.7 loops memory-leaks generator django-queryset

我有以下django视图方法:

def csv_generator(self):
    csv_writer = unicodecsv.writer(
        output_csv_string,
        delimiter=settings.CSV_DELIMITER,
        encoding='utf-8')

    output_csv_string = StringIO()
    start_id = 0
    while True:
        qs = self.get_queryset().filter(id__gt=start_id)
        qs = qs.order_by('pk')[:settings.CSV_CHUNK_SIZE]
        qs = qs.fetch_all_related()

        if not qs:
            raise StopIteration()

        for obj in qs:
            start_id = start_id if obj.id < start_id else obj.id
            csv_writer.writerow(self.get_csv_list(obj))
            yield output_csv_string.getvalue()
            output_csv_string.truncate(0)

通过这样的视图调用:

response = HttpResponse(self.csv_generator(), mimetype='text/csv')
return response

qs.fetch_all_related()获取的查询集不会释放内存,并且在后续请求期间进程内存会耗尽。生成器在这里有什么问题?

0 个答案:

没有答案