V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
ryanking8215
V2EX  ›  Python

我觉得 flask-sqlalchemy 的实现是不是有其局限性?

  •  
  •   ryanking8215 ·
    ryanking8215 · 2015-02-12 17:22:21 +08:00 · 3200 次点击
    这是一个创建于 3364 天前的主题,其中的信息可能已经有所发展或是发生改变。
    flask-sqlalchemy把它的执行上下文绑定在了flask的app的context上。
    但sqlalchemy本身提供了scoped_session.

    如果把一个线程看成一个目录,那么其下有app.context栈和sqlalchemy.scoped_session2个资源。
    现在他的实现,把sqlalchemy.scoped_session的资源放在了app.context栈下。

    如果我需要自己起个worker线程来处理redis的消息并写入数据库,那么flask-sqlalchemy的db对象就没有办法在自己的线程里执行了。

    app.xn--teardown_appcontext,session-8x78c.xn--remove()ok-3m3p752k.">相反如果把db的实现和app的context脱离开来,就灵活多了。反正只要注册一下@app.teardown_appcontext,把session.remove()就ok了。
    目前尚无回复
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   5886 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 33ms · UTC 02:29 · PVG 10:29 · LAX 19:29 · JFK 22:29
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.