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)
 

猜你喜欢

转载自blog.csdn.net/weixin_42020284/article/details/83785661
今日推荐