How to make SQLAlchemy in Tornado to be async?

前端 未结 5 432
花落未央
花落未央 2020-12-02 05:11

How to make SQLAlchemy in Tornado to be async ? I found example for MongoDB on async mongo example but I couldn\'t find anything like

5条回答
  •  无人及你
    2020-12-02 05:55

    I am using tornado with sqlalchemy in next way:

    
    from tornado_mysql import pools
    from sqlalchemy.sql import table, column, select, join
    from sqlalchemy.dialects import postgresql, mysql
    
    # from models import M, M2
    
    t = table(...)
    t2 = table(...)
    
    xxx_id = 10
    
    j = join(t, t2, t.c.t_id == t2.c.id)
    s = select([t]).select_from(j).where(t.c.xxx == xxx_id)
    
    sql_str = s.compile(dialect=mysql.dialect(),compile_kwargs={"literal_binds": True})
    
    
    pool = pools.Pool(conn_data...)
    cur = yield pool.execute(sql_str)
    data = cur.fetchone()
    
    

    In that case we are able to use sqlalchemy models, and sqlalchemy tools for constructig queries.

提交回复
热议问题