推荐学习书目
› 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
wdg8106
V2EX  ›  Python

tornado ,sqlalchemy 怎么做数据库的读写分离

  •  
  •   wdg8106 · Feb 29, 2016 · 6381 views
    This topic created in 3912 days ago, the information mentioned may be changed or developed.
    网站框架用的是 tornado ,与数据库的连接用的是 sqlalchemy,现在访问量有点大,加上有些查询语句效率不是很高,导致访问速度有点下降,现在想做读写分离来缓解下主数据库的压力。
    之前的写法是在一次请求前建立一个 session,然后请求结束后结束这个 session ,那如果做读写分离是不是就要在一次请求时维护两个 session,一个负责读,一个负责写,请问大家有谁做过这个呢,有什么比较好的方法呢,请指点一下啊~~
    6 replies  •  2016-03-07 14:06:12 +08:00
    jixiangqd
    jixiangqd
    Feb 29, 2016❤️1
    1
    mycat 、 oneproxy 可以试试这种对应用层透明的中间件。。。
    MasterYoda
    MasterYoda
    Feb 29, 2016❤️1
    2
    http://stackoverflow.com/questions/8947918/read-slave-read-write-master-setup
    作者自己的答案。
    他博客里还写过别的方法,判断 is_flush 的,在底层做读写分离。
    wdg8106
    wdg8106
    OP
    Mar 1, 2016
    3
    @MasterYoda 太感谢了,困扰了好长时间,终于有解决思路了
    wdg8106
    wdg8106
    OP
    Mar 1, 2016
    4
    @jixiangqd 只试过 mysql-proxy ,跟 sqlalchemy 结合使用效果不是很好,你说的这两个中间件是类似的吗,我再自己看看
    SlipStupig
    SlipStupig
    Mar 2, 2016❤️1
    5
    @wdg8106 mysql-proxy 是神坑,可以用一下 360 的 atlas
    siteshen
    siteshen
    Mar 7, 2016❤️1
    6
    继承实现 Session.get_bind 方法就行:

    def get_bind(self, mapper=None, clause=None):
    # 增强版,支持 User.use_master().get(1024)
    # 当然还需要实现 Query._execute_and_instances ,增加 Query.use_master 方法
    # if use_master:
    # return bind['master']

    if not self._flushing and bind['slaves']:
    return random.choice(bind['slaves'])
    else:
    return bind['master']
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   970 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 22:54 · PVG 06:54 · LAX 14:54 · JFK 17:54
    ♥ Do have faith in what you're doing.