推荐学习书目
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 · Feb 12, 2015 · 3871 views
    This topic created in 4131 days ago, the information mentioned may be changed or developed.
    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了。
    No Comments Yet
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   894 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 21:49 · PVG 05:49 · LAX 14:49 · JFK 17:49
    ♥ Do have faith in what you're doing.