标签: aio-mysql

我是否需要跟踪 asyncio 事件循环,或者我可以在需要时调用 asyncio.get_event_loop 吗?

我正在使用aiohttp.

一些协程需要使用库来调用aiomysql数据库。的文档aiomysql有以下示例:

import asyncio
import aiomysql

loop = asyncio.get_event_loop()


async def test_example():
    conn = await aiomysql.connect(host='127.0.0.1', port=3306,
                                       user='root', password='', db='mysql',
                                       loop=loop)

    cur = await conn.cursor()
    await cur.execute("SELECT Host,User FROM user")
    print(cur.description)
    r = await cur.fetchall()
    print(r)
    await cur.close()
    conn.close()

loop.run_until_complete(test_example())
Run Code Online (Sandbox Code Playgroud)

我的问题是关于全局变量的定义loop

loop = asyncio.get_event_loop()
Run Code Online (Sandbox Code Playgroud)

我真的需要将其loop作为全局变量保存在某个地方,还是可以asyncio.get_event_loop()在需要时调用它?

例如,在上面的代码示例中,当我连接到数据库时,我可以获得事件循环:

    conn = await aiomysql.connect(host='127.0.0.1', port=3306,
                                       user='root', password='', db='mysql',
                                       loop=asyncio.get_event_loop())
Run Code Online (Sandbox Code Playgroud)

调用是否会产生不小的运行时成本asyncio.get_event_loop()或我遗漏的其他内容?

python python-asyncio aiohttp aio-mysql

6
推荐指数
1
解决办法
995
查看次数

asyncio 错误地警告流对象被垃圾收集;显式调用“stream.close()”

我正在使用 python3.8 的内置 asyncio 包和已安装的 aiomysql 包实现异步 MySQL 查询执行。即使我已经正确关闭了所有打开的游标和连接,我的控制台上仍然会出现如下错误消息。

An open stream object is being garbage collected; call "stream.close()" explicitly.
Run Code Online (Sandbox Code Playgroud)

下面给出了代码的摘要......

#db.py

import asyncio

class AsyncMysqlSession:

    def __init__(self, loop, db_settings=DEFAULTDB):
        self.db_settings = db_settings
        self.loop = loop

    async def __aenter__(self):
        self.conn = await aiomysql.connect(host=self.db_settings['HOST'],
                                       port=self.db_settings['PORT'],
                                       user=self.db_settings['USER'],
                                       password=self.db_settings['PASSWORD'],
                                       db=self.db_settings['NAME'],
                                       loop=self.loop)
        self.cursor = await self.conn.cursor(aiomysql.cursors.DictCursor)
        return self

    async def __aexit__(self, exception, value, traceback):
        await self.cursor.close()
        self.conn.close()

    async def query(self, sql, *args):
        await self.cursor.execute(sql, values)
        await self.conn.commit()
        rows = await self.cursor.fetchall()
        return list(rows)


async …
Run Code Online (Sandbox Code Playgroud)

python python-3.x async-await python-asyncio aio-mysql

5
推荐指数
1
解决办法
598
查看次数

我应该如何设置 aiomysql 池缓存?

当我使用mysql_poolfromaiomysql更新一条数据时,第一次和第二次是一样的。

\n
class Test(object):\n    async def _pool(self):\n        self.pool = await aiomysql.create_\xc3\xa7(**mysql_options)\n\n    async def get_one(self, sql, param=None):\n        await self.cur.execute(sql, param)\n        result = await self.cur.fetchone()\n        return result\n\n    async def get(self):\n        self.conn = await self.pool.acquire()\n        self.cur = await self.conn.cursor(DictCursor)\n        sql = \'\'\'select policy from tb_user where id = 2;\'\'\'\n        res = await self.get_one(sql)\n        print(res)\n        await self.cur.close()\n        await self.pool.release(self.conn)\n\n    @staticmethod\n    def update():\n        import pymysql\n        coon = pymysql.connect(host=\'127.0.0.1\',\n                               port=3306,\n                               user=mysql_options[\'user\'],\n                               autocommit=True,\n                               password=mysql_options[\'password\'],\n                               database=mysql_options[\'db\'])\n        cursor = coon.cursor()\n        sql = \'\'\'update tb_user set …
Run Code Online (Sandbox Code Playgroud)

python mysql python-asyncio aio-mysql

5
推荐指数
1
解决办法
1186
查看次数

当另一个协程已经在等待传入数据时调用 readexactly()

使用池连接 - aiomysql

我的代码如下所示:

# POOL CONNECTION

# create pool connection
async def create_pool():
    print("Creating pool connection...")
    global pool

    pool = await aiomysql.create_pool(
        host=DB_HOST,
        port=DB_PORT,
        user=DB_USER,
        password=DB_PASSWORD,
        db=DB_DBNAME,
        autocommit=True
    )


async def get_connection():
    async with pool.acquire() as conn:
        return conn
    pool.close()
    await pool.wait_closed()


connection = await get_connection()
async with connection.cursor() as cursor:
            await cursor.execute(...)
Run Code Online (Sandbox Code Playgroud)

如果发出单个请求,连接到 mysql,它会正确运行,但如果同时发出 2 个或更多请求,则会崩溃并抛出此错误:

当另一个协程已经在等待传入数据时调用 readexactly()

python mysql connection-pooling pool aio-mysql

5
推荐指数
0
解决办法
1193
查看次数