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

查看:91
本文介绍了我如何知道是否可以禁用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事件系统,因此可以安全地关闭它.您需要审核代码以进行验证-您正在寻找与> c3>或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事件系统(并禁用警告),只需添加:

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

SQLALCHEMY_TRACK_MODIFICATIONS = False

更改您的应用程序配置,直到更改默认设置为止(很有可能在Flask-SQLAlchemy v3中).

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

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

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

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

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

据我了解,更改的理由有三点:

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

  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天全站免登陆