我怎么知道我是否可以禁用 SQLALCHEMY_TRACK_MODIFICATIONS? [英] How do I know if I can disable SQLALCHEMY_TRACK_MODIFICATIONS?

查看:21
本文介绍了我怎么知道我是否可以禁用 SQLALCHEMY_TRACK_MODIFICATIONS?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我运行使用 Flask-SQLAlchemy 的应用程序时,我都会收到以下警告:SQLALCHEMY_TRACK_MODIFICATIONS 选项将被禁用.

Every time I run my app that uses Flask-SQLAlchemy I get the following warning that the SQLALCHEMY_TRACK_MODIFICATIONS option will be disabled.

/home/david/.virtualenvs/flask-sqlalchemy/lib/python3.5/site-packages/flask_sqlalchemy/__init__.py:800: UserWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True to suppress this warning.
  warnings.warn('SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future.  Set it to True to suppress this warning.')

我试图找出这个选项的作用,但 Flask-SQLAlchemy 文档并不清楚是什么使用了这种跟踪.

I tried to find out what this option does, but the Flask-SQLAlchemy documentation isn't clear about what uses this tracking.

SQLALCHEMY_TRACK_MODIFICATIONS

如果设置为 True(默认),Flask-SQLAlchemy 将跟踪对象的修改并发出信号.这需要额外的内存,如果不需要,可以禁用.

If set to True (the default) Flask-SQLAlchemy will track modifications of objects and emit signals. This requires extra memory and can be disabled if not needed.

如何确定我的项目是否需要 SQLALCHEMY_TRACK_MODIFICATIONS = True 或者我是否可以安全地禁用此功能并节省服务器内存?

How do I find out if my project requires SQLALCHEMY_TRACK_MODIFICATIONS = True or if I can safely disable this feature and save memory on my server?

推荐答案

很可能您的应用程序没有使用 Flask-SQLAlchemy 事件系统,因此您可以安全地关闭.您需要审核代码以进行验证——您正在寻找任何与 <挂钩的内容code>models_committed 或 before_models_committed.如果您确实发现您正在使用 Flask-SQLAlchemy 事件系统,您可能应该更新代码以使用 SQLAlchemy 的内置事件系统.

Most likely your application doesn't use the Flask-SQLAlchemy event system, so you're probably safe to turn off. You'll need to audit the code to verify--you're looking for anything that hooks into models_committed or before_models_committed. If you do find that you're using the Flask-SQLAlchemy event system, you probably should update the code to use SQLAlchemy's built-in event system instead.

Flask-SQLAlchemy 2.1 的默认值是None,这是一个假值,所以事件系统被禁用.在旧版本中,默认值为 True,因此您需要明确禁用它.

The default value as of Flask-SQLAlchemy 2.1 is None, which is a falsy value, so the event system is disabled. In older versions, the default value was True, so you'll need to explicitly disable it.

但是,在这两种情况下,警告都不会被消除,直到将其显式设置为 False.为此,请添加:

However, in both cases, the warning won't be silenced until this is explicitly set to False. To do that, add:

SQLALCHEMY_TRACK_MODIFICATIONS = False

到您的应用配置.

背景——这是警告告诉您的内容:

Background--here's what the warning is telling you:

Flask-SQLAlchemy 有自己的事件通知系统,它位于 SQLAlchemy 之上.为此,它会跟踪对 SQLAlchemy 会话的修改.这需要额外的资源,因此选项 SQLALCHEMY_TRACK_MODIFICATIONS 允许您禁用修改跟踪系统.

Flask-SQLAlchemy has its own event notification system that gets layered on top of SQLAlchemy. To do this, it tracks modifications to the SQLAlchemy session. This takes extra resources, so the option SQLALCHEMY_TRACK_MODIFICATIONS allows you to disable the modification tracking system.

改变的理由有三个:

  1. 没有多少人使用 Flask-SQLAlchemy 的事件系统,但大多数人没有意识到他们可以通过禁用它来节省系统资源.所以更明智的默认设置是禁用它,想要它的人可以打开它.

  1. Not many people use Flask-SQLAlchemy's event system, but most people don't realize they can save system resources by disabling it. So a saner default is to disable it and those who want it can turn it on.

Flask-SQLAlchemy 中的事件系统有很多问题(请参阅下面提到的拉取请求中链接的问题),需要对很少有人使用的功能进行额外维护.

The event system in Flask-SQLAlchemy has been rather buggy (see issues linked to in the pull request mentioned below), requiring additional maintenance for a feature that few people use.

在 v0.7 中,SQLAlchemy 本身添加了一个强大的事件系统 包括创建自定义事件的能力.理想情况下,Flask-SQLAlchemy 事件系统应该只做一些自定义的 SQLAlchemy 事件挂钩和侦听器,然后让 SQLAlchemy 自己管理事件触发器.

In v0.7, SQLAlchemy itself added a powerful event system including the ability to create custom events. Ideally, the Flask-SQLAlchemy event system should do nothing more than create a few custom SQLAlchemy event hooks and listeners, and then let SQLAlchemy itself manage the event trigger.

您可以在关于开始触发此警告的拉取请求的讨论中看到更多信息.

这篇关于我怎么知道我是否可以禁用 SQLALCHEMY_TRACK_MODIFICATIONS?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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