Python 我怎么知道我是否可以禁用 SQLALCHEMY_TRACK_MODIFICATIONS?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33738467/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 13:52:18  来源:igfitidea点击:

How do I know if I can disable SQLALCHEMY_TRACK_MODIFICATIONS?

pythonflasksqlalchemyflask-sqlalchemy

提问by Robert

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

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

/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.')

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

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

SQLALCHEMY_TRACK_MODIFICATIONS

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(默认),Flask-SQLAlchemy 将跟踪对象的修改并发出信号。这需要额外的内存,如果不需要可以禁用。

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

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

采纳答案by Jeff Widman

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_committedor 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 事件系统,因此您可以安全地关闭它。您需要审核代码以进行验证——您正在寻找与models_committedbefore_models_committed挂钩的任何内容。如果您确实发现您正在使用 Flask-SQLAlchemy 事件系统,您可能应该更新代码以使用 SQLAlchemy 的内置事件系统。

To turn off the Flask-SQLAlchemy event system (and disable the warning), just add:

要关闭 Flask-SQLAlchemy 事件系统(并禁用警告),只需添加:

SQLALCHEMY_TRACK_MODIFICATIONS = False

to your app config until the default is changed (most likely in Flask-SQLAlchemy v3).

到您的应用程序配置,直到更改默认值(最有可能在 Flask-SQLAlchemy v3 中)。



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

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

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_MODIFICATIONSallows you to disable the modification tracking system. Currently the option defaults to True, but in the future, that default will change to False, thereby disabling the event system.

Flask-SQLAlchemy 有自己的事件通知系统,它位于 SQLAlchemy 之上。为此,它会跟踪对 SQLAlchemy 会话的修改。这需要额外的资源,因此该选项SQLALCHEMY_TRACK_MODIFICATIONS允许您禁用修改跟踪系统。目前该选项默认为True,但将来,该默认值将更改为False,从而禁用事件系统。

As far as I understand, the rationale for the change is three-fold:

据我了解,改变的理由有三个:

  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.

  2. 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.

  3. In v0.7, SQLAlchemy itself added a powerful event systemincluding 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.

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

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

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

You can see more in the discussion around the pull request that started triggering this warning.

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

回答by Pitto

Jeff Widman's detailed explanation is simply perfect.

Jeff Widman 的详细解释简直完美。

Since I had some copy'n'paste fights before getting this right I'd like to make it easier for the next one that will be in my shoes.

由于在正确处理之前我进行了一些复制粘贴的斗争,因此我想让下一个将在我的鞋子中变得更容易。

In your code, immediately after:

在您的代码中,紧随其后

app = Flask(__name__)

If you want to enable track modifications simply add:

如果要启用轨道修改,只需添加:

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True

Otherwise, if you are notusing this feature, you may want to change the value to False in order not to waste system resources. This will still silence the warning since you're anyway explicitly setting the config.

否则,如果您使用此功能,您可能希望将该值更改为 False,以免浪费系统资源。这仍然会使警告静音,因为您无论如何都明确设置了配置。

Here's the same snippet with False value:

这是带有 False 值的相同片段:

app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

Thanks to Jeff Widman for this added suggestion and details.

感谢 Jeff Widman 提供的这个增加的建议和细节。

回答by jasonrhaas

The above answers look good. However, I wanted to point out this line in the Flask-SQLAlchemy documentation because I was still getting these warnings after setting SQLALCHEMY_TRACK_MODIFICATIONS = Falsein my application config.

上面的答案看起来不错。但是,我想在 Flask-SQLAlchemy 文档中指出这一行,因为SQLALCHEMY_TRACK_MODIFICATIONS = False在我的应用程序配置中设置后我仍然收到这些警告。

On this page: http://flask-sqlalchemy.pocoo.org/2.3/config/

在此页面上:http: //flask-sqlalchemy.pocoo.org/2.3/config/

The following configuration values exist for Flask-SQLAlchemy. Flask-SQLAlchemy loads these values from your main Flask config which can be populated in various ways. Note that some of those cannot be modified after the engine was created so make sure to configure as early as possible and to not modify them at runtime.

Flask-SQLAlchemy 存在以下配置值。Flask-SQLAlchemy 从您的主 Flask 配置中加载这些值,这些值可以通过各种方式填充。请注意,其中一些在引擎创建后无法修改,因此请确保尽早配置并且不要在运行时修改它们。

In other words, make sure to set up your app.configbeforecreating your Flask-SQLAlchemy database.

换句话说,请确保app.config创建 Flask-SQLAlchemy 数据库之前进行设置。

For example, if you are configuring your application to set SQLALCHEMY_TRACK_MODIFICATIONS = False:

例如,如果您将应用程序配置为设置SQLALCHEMY_TRACK_MODIFICATIONS = False

from flask import Flask
app = Flask(__name__)
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False

db = SQLAlchemy(app)