关闭SQL日志记录同时保留settings.DEBUG? [英] turn off SQL logging while keeping settings.DEBUG?

查看:140
本文介绍了关闭SQL日志记录同时保留settings.DEBUG?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在设置时,Django将SQL操作记录到内部缓冲区(无论是否记录到文件).DEBUG = True。因为我有长时间运行的进程做了大量的数据库操作,所以这使得我的开发模式实例的程序在内存消耗上快速增长。

Django logs SQL operations to an internal buffer (whether logging to file or not) when settings.DEBUG=True. Because I have long-running process that does a lot of DB operations, this causes my development-mode instances of the program to grow in memory consumption very quickly.

我会喜欢禁用内部SQL记录机制,同时留下settings.DEBUG打开我的开发:这是可能吗?

I would like to disable the internal SQL logging mechanism while leaving settings.DEBUG turned on for my development: is this possible?

Django版本1.3.0。

Django version 1.3.0.

推荐答案

当设置.DEBUG为True时,Django使用CursorDebugWrapper而不是CursorWrapper。这是将查询附加到connection.queries并消耗内存。我将猴子修补连接包装器,以始终使用CursorWrapper:

When settings.DEBUG is True, Django uses CursorDebugWrapper instead of CursorWrapper. This is what appends the queries to connection.queries and consumes memory. I would monkey-patch the connection wrapper to always use CursorWrapper:

from django.conf import settings
from django.db.backends import BaseDatabaseWrapper
from django.db.backends.util import CursorWrapper

if settings.DEBUG:
    BaseDatabaseWrapper.make_debug_cursor = lambda self, cursor: CursorWrapper(cursor, self)

像其他人一样禁用日志记录将无法解决问题,因为CursorDebugWrapper仍然存储连接中的查询.queries即使日志记录已关闭。

Disabling logging like others suggest won't fix the problem, because CursorDebugWrapper still stores the queries in connection.queries even if logging is off.

这篇关于关闭SQL日志记录同时保留settings.DEBUG?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆