1. 程式人生 > >tornado mysql 非同步

tornado mysql 非同步

# -*- coding:utf-8 -*-
"""1.0"""

from tornado import ioloop, gen
from tornado_mysql import pools


POOL = pools.Pool(
    dict(host='127.0.0.1', port=3306, user='root', passwd='mysql', db='spider_ivy'),
    max_idle_connections=2,
    max_recycle_sec=3
)


# 查詢資料
@gen.coroutine
def get():
    cur = yield POOL.execute("SELECT * from news")
    print(cur.fetchall())


# 插入資料
@gen.coroutine
def insert():
    # cur = yield POOL.execute("""insert into news(name) values ('pkq')""")
    yield POOL.execute("""insert into news(name) values ('kk')""")
    # print(cur)
    # return cur


# 修改資料
@gen.coroutine
def update():
    yield POOL.execute("""update news set name = 'qi' where id = 38; """)


# 刪除資料
@gen.coroutine
def delete():
    yield POOL.execute("""delete from news where id = 36;""")


@gen.coroutine
def main():
    workers = delete()
    yield workers


if __name__ == '__main__':
    ioloop.IOLoop.current().run_sync(main)