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
wdg8106
V2EX  ›  Python

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

  •  
  •   wdg8106 · 2016-02-29 16:08:53 +08:00 · 4974 次点击
    这是一个创建于 2978 天前的主题,其中的信息可能已经有所发展或是发生改变。
    网站框架用的是 tornado ,与数据库的连接用的是 sqlalchemy,现在访问量有点大,加上有些查询语句效率不是很高,导致访问速度有点下降,现在想做读写分离来缓解下主数据库的压力。
    之前的写法是在一次请求前建立一个 session,然后请求结束后结束这个 session ,那如果做读写分离是不是就要在一次请求时维护两个 session,一个负责读,一个负责写,请问大家有谁做过这个呢,有什么比较好的方法呢,请指点一下啊~~
    6 条回复    2016-03-07 14:06:12 +08:00
    jixiangqd
        1
    jixiangqd  
       2016-02-29 17:10:20 +08:00   ❤️ 1
    mycat 、 oneproxy 可以试试这种对应用层透明的中间件。。。
    MasterYoda
        2
    MasterYoda  
       2016-02-29 20:35:16 +08:00   ❤️ 1
    http://stackoverflow.com/questions/8947918/read-slave-read-write-master-setup
    作者自己的答案。
    他博客里还写过别的方法,判断 is_flush 的,在底层做读写分离。
    wdg8106
        3
    wdg8106  
    OP
       2016-03-01 16:46:36 +08:00
    @MasterYoda 太感谢了,困扰了好长时间,终于有解决思路了
    wdg8106
        4
    wdg8106  
    OP
       2016-03-01 16:47:55 +08:00
    @jixiangqd 只试过 mysql-proxy ,跟 sqlalchemy 结合使用效果不是很好,你说的这两个中间件是类似的吗,我再自己看看
    SlipStupig
        5
    SlipStupig  
       2016-03-02 13:52:57 +08:00   ❤️ 1
    @wdg8106 mysql-proxy 是神坑,可以用一下 360 的 atlas
    siteshen
        6
    siteshen  
       2016-03-07 14:06:12 +08:00   ❤️ 1
    继承实现 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']
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3241 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 26ms · UTC 13:15 · PVG 21:15 · LAX 06:15 · JFK 09:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.