用异步aiomysql处理数据库操作程序速度应该有所提升,一般都是和aiohttp, asyncio一起使用滴。
代码如下
logging.basicConfig(level=logging.DEBUG, # 设置日志显示级别 filename="test1.log", format='%(asctime)s[line:%(lineno)d] %(levelname)s %(message)s', datefmt='%A, %d %B %Y %H:%M:%S', # 指定日期时间格式 ) # 指定handler使用的日志显示格式 class Aiomysql_DbHelper(object): def __init__(self, host, user, pwd, db): # self._conn = None # self._cursor = None self._pool = None self.host = host self.user = user self.pwd = pwd self.db = db def __new__(cls, *args, **kwargs): if not hasattr(Aiomysql_DbHelper, '_instance'): Aiomysql_DbHelper._instance = object.__new__(cls) return Aiomysql_DbHelper._instance async def get_pool(self): if not self._pool: self._pool = await aiomysql.create_pool(host=self.host, port=3306, user=self.user, password=self.pwd, db=self.db) return self._pool async def fetch(self, sql, data=None, excutemany=0): try: pool = await self.get_pool() async with pool.acquire() as conn: async with conn.cursor(aiomysql.DictCursor) as cursor: try: if not excutemany: res = await cursor.execute(sql, data) else: res = await cursor.executemany(sql, data) await conn.commit() if 'select' in 'sql': return await cursor.fetchall() return res except pymysql.err.IntegrityError as e: await conn.rollback() logging.info(f"mysql insert 异常:{traceback.format_exc()}") except ConnectionError and pymysql.err.OperationalError as ce: logging.info(f"mysql 链接 异常:{traceback.format_exc()}") except: logging.info(f"mysql {sql.split(' ')[0]} 异常:{traceback.format_exc()}") def __repr__(self): return f'Aiomysql_DbHelper(主机:{self.host}, 用户名:{self.user}, 数据库:{self.db})' __str__ = __repr__
是一个单例类,使用__new__来实现饿汉式单例类,且创建了连接池,每次从连接池取出一个连接来进行操作。(最上面的是python标准库里的logging日志类,会把loggin.info保存到test1.log里面)
特殊方法__repr__则是print()调用的方法,__str__是str()调用的方法
初学python如果有什么错误和不足,欢迎指正。