tornado+peewee

前言:
  • 需要异步操作MySQL,又要用orm,使用sqlalchemy需要加celery,觉得比较麻烦,选择了peewee-async
开发环境 python3.6.8+peewee-async0.5.12+peewee2.10.2
  • 数据库:MySQL,使用peewee-async需要依赖库 pip install aiomysql

  • peewee-async,对peewee版本只支持peewee<=2.10.2,>=2.8.0

  • python3.5以后使用async和await关键字实现原生协程,也可以使用tornado的gen模块下coroutine实现协程,或者asyncio模块实现协程,下文统一使用async和await

tornado 异步调用MySQL

  • 爬坑

    • 最初遇到的坑,使用了最新版的peewee,连接池连接,也使用了async和await协程,但是怎么调用都会阻塞,后来发现不是阻塞单个协程,是阻塞了整个进程,因为tornado是单进程,必须数据库也使用异步操作,才能不阻塞整个进程

    • pip install peewee-async 的时候默认会安装符合版本要求的peewee

    • 查看peewee-async 模块的MySQLDatabase,继承了AsyncDatabase和peewee.MySQLDatabase,AsyncDatabase方法全部使用协程实现异步

  • peewee-async 连接MySQL,返回database对象

    • 单连接

      1import peewee_async 2# db = peewee_async.MySQLDatabase(database_name, host, port, user, password) 3# 或者,将自己的数据库信息封装到字典中 4db_setting = { 5 "user": "root", 6 "password": "xxxxxx", 7 "host": "127.0.0.1", 8 "port": 3306, 9 "database": "test" 10 } 11db = peewee_async.MySQLDatabase(**db_setting)
    • 连接池

      1from playhouse.shortcuts import RetryOperationalError 2from peewee_async import PooledMySQLDatabase 3# 可以自动重新连接的连接池 4class RetryMySQLDatabase(RetryOperationalError, PooledMySQLDatabase): 5 _instance = None 6 7 @staticmethod 8 def get_db_instance(): 9 if not RetryMySQLDatabase._instance: 10 RetryMySQLDatabase._instance = RetryMySQLDatabase(database_name, 11 host, port, user, password, 12 max_connections=10) 13 return RetryMySQLDatabase._instance 14 15db = RetryMySQLDatabase.get_db_instance()
  • 返回的database对象的一些常用方法

    • get_tables() 返回列表,当前数据库的所有表名

    • get_columns(table_name) 传参表名,返回列表,包含ColumnMetadata对象,字段信息

    • create_tables()

      第一个参数为列表,包含要创建的表model 第二个参数safe, 不传默认为False,建表的时候如果表已经存在会报错,可以加safe=True

    • is_closed() 判断当前连接是否关闭

    • close() 关闭连接

  • peewee

    • peewee 模块可以参照官方文档用法,和sqlalchemy差别不大,为了下面的操作,暂时建一个model

    book.py

    1# 集中写一个basemodel,将数据库对象绑定在model上,类才能映射到数据库中的表 2class BaseModel(Model): 3 class Meta: 4 database = db 5class Book(BaseModel): 6 book_id = PrimaryKeyField() # int 主键自增,在peewee3.10 版本中新增了字段AutoField,表示主键自增 7 book_name = CharField(max_length=100, verbose_name="书名") 8 # 鉴于篇幅, 作者表不写,外键第一个参数为model类名,to_field表示关联的字段 9 book_auth = ForeignKeyField(User, to_field="user_id", verbose_name="作者id")
  • peewee-async Manager

    • 管理数据库操作,实现异步操作数据库必须使用Manager,查看源码可以看到,类中的get, create, execute等方法都是使用装饰器@asyncio.coroutine加yield from,在原有的数据库操作基础上做了封装

    • 初始化传入数据库连接对象,生成manager对象,使用该对象完成数据库操作,在tornado中一般选择绑定到app上

      from tornado.web import RequestHandler from tornado import gen import tornado.ioloop from book import Book

      class RegHandler(RequestHandler): async def get(self): # 在handler类中可以使用绑定到app上的manager对象执行操作,因为是异步操作需要使用await关键字 # 以下两种查询方式返回结果对象格式不同 book_res = await self.application.objects.get(Book, book_name="简爱") id = book_res.book_id # 只有调用了execute方法才是执行,query打印可以看到只是生成了sql语句 # 为保证异步调用必须使用peewee-async manager生成的对象执行操作函数,不能使用model的execute执行,会同步阻塞 query = Book.select().where(Book.username=="简爱") # execute执行返回AsyncQueryWrapper对象,如果有值可以通过下标取出每个book对象 # query 对象执行前可以调用dicts()方法,返回对象内容为字典格式 # query.tuples() 返回对象内容为元组格式,相当于sqlalchemy,fetchall() # 其他方法或者属性有需要的可以使用dir()方法和getattr()方法查看属性,以及属性调用后返回值 book_res = await self.application.objects.execute(query.dicts()) pass async def post(self): from tornado.escape import json_decode body = json_decode(self.request.body) # 增 # 如果参数是字典格式,且key值对应字段名称,可以使用peewee model里面的insert方法 await self.application.objects.execute(Book.insert(body)) # 或者使用封装的create方法,create方法源码还是调用了model类的insert方法 await self.application.objects.create(Book, boo_name=body.get("book"), book_auth=2) pass

      app = tornado.web.Application([ (r"/book", BookHandler) ])

      if name == 'main': app = tornado.web.Application(urlpaten) import peewee_async # 将manager对象绑定到app上 app.objects = peewee_async.Manager(database) server = httpserver.HTTPServer(app, xheaders=True) server.listen(80) tornado.ioloop.IOLoop.current().start()


  • 初步介绍先先写到这里,通过上述介绍使用peewee和peewee-async没有大问题,后面会通过详细功能具体详细介绍使用,细小的api建议看官方文档

  • 有问题欢迎指出,随时修正

点赞
收藏

评论区

加载中...

相关推荐

MySQL:[Err] 1292 - Incorrect datetime value: ‘0000-00-00 00:00:00‘ for column ‘CREATE_TIME‘ at row 1

文章目录问题用navicat导入数据时,报错:原因这是因为当前的MySQL不支持datetime为0的情况。解决修改sql\mode:sql\mode:SQLMode定义了MySQL应支持的SQL语法、数据校验等,这样可以更容易地在不同的环境中使用MySQL。全局s

Oracle 分组与拼接字符串同时使用

SELECTT.,ROWNUMIDFROM(SELECTT.EMPLID,T.NAME,T.BU,T.REALDEPART,T.FORMATDATE,SUM(T.S0)S0,MAX(UPDATETIME)CREATETIME,LISTAGG(TOCHAR(

MySQL部分从库上面因为大量的临时表tmp_table造成慢查询

背景描述Time:20190124T00:08:14.70572408:00User@Host:@Id:Schema:sentrymetaLast_errno:0Killed:0Query_time:0.315758Lock_

手写Java HashMap源码

HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程HashMap的使用教程22

2020年前端实用代码段,为你的工作保驾护航

有空的时候,自己总结了几个代码段,在开发中也经常使用,谢谢。1、使用解构获取json数据let jsonData  id: 1,status: "OK",data: 'a', 'b';let  id, status, data: number   jsonData;console.log(id, status, number )

Python3:sqlalchemy对mysql数据库操作,非sql语句

Python3:sqlalchemy对mysql数据库操作,非sql语句python3authorlizmdatetime2018020110:00:00coding:utf8'''