aiomysql:

  1.异步连接

  2.异步单例

import asyncio
import aiomysql

loop = asyncio.get_event_loop()


async def getconnection():

    conn = await aiomysql.connect(host="127.0.0.1", port=3306,
                                  user="root", password="root",
                                  db="mysql", loop=loop)

    if conn:
        print('connect correct')
        return conn
    else:
        raise("connect to mysql error ")
        conn.close

loop.run_until_complete(getconnection())

异步连接数据库

class Pmysql:
    """docstring for Pmydql."""
    __connection = None

    def __init__(self, ):
        self.conn = None
        self.cursor = None

    @staticmethod
    async def getconnection():
        if not Pmysql.__connection:
            conn = await aiomysql.connect(host='127.0.0.1', port=3306,
                                          user='root', password='root',
                                          db='youku')
            if conn:
                Pmysql.__connection = conn
                print('connect to mysql correct!')
                return conn
            else:
                raise("connect to mysql error ")
        else:
            return Pmysql.__connection

aiomysql异步单例版本

 

相关文章:

  • 2022-12-23
  • 2021-12-26
  • 2021-12-30
  • 2022-12-23
  • 2021-09-21
  • 2021-09-22
  • 2022-12-23
  • 2021-10-01
猜你喜欢
  • 2021-09-19
  • 2022-12-23
  • 2022-01-04
  • 2021-11-20
  • 2021-08-07
相关资源
相似解决方案