Discuss / Python / 总算功能都测试了一遍,评论里的问题有幸全遇到了一遍。

总算功能都测试了一遍,评论里的问题有幸全遇到了一遍。

Topic source

starwind徐

#1 Created at ... [Delete] [Delete and Lock User]

总结一下

1、无法连接到sql服务

pymysql.err.OperationalError: (2003, "Can't connect to MySQL server on 'localhost'")

可能的原因 没有提前创建数据库 mysql> -u root -p < schema.sql 操作系统里面执行这行代码,mysql命令里执行schema.sql中的代码 2、Event loop is closed 修改execute函数,加上关闭。

async def execute(sql, args, autocommit=True):
    log(sql)
    async with __pool.get() as conn:
        if not autocommit:
            await conn.begin()
        try:
            async with conn.cursor(aiomysql.DictCursor) as cur:
                await cur.execute(sql.replace('?', '%s'), args)
                affected = cur.rowcount
                await cur.close()
            if not autocommit:
                await conn.commit()
        except BaseException as e:
            if not autocommit:
                await conn.rollback()
            raise
        finally:
            conn.close()
        return affected

-__-__-____-

#2 Created at ... [Delete] [Delete and Lock User]

感谢,最初还以为async with ... as ...会和with open(...) as ...一样自动关闭,看来是我想多了,楼下有人写了一个destroy_pool在test完之后调用,应该是异曲同工。


  • 1

Reply