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

Ale*_*lex 5 python mysql python-asyncio aio-mysql

当我使用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 policy = 9 where id = 2;\'\'\'\n        cursor.execute(sql)\n        sql = \'\'\'select policy from tb_user where id = 2;\'\'\'\n        cursor.execute(sql)\n        data = cursor.fetchone()\n        print(data)\n    async def run(self):\n        await self._pool()\n        await self.get()\n        self.update()\n        await self.get()\n\n\nif __name__ == \'__main__\':\n    test = Test()\n    loop = asyncio.get_event_loop()\n    loop.run_until_complete(test.run())\n\n
Run Code Online (Sandbox Code Playgroud)\n

版本

\n
    \n
  • 蟒蛇3.7.0
  • \n
  • aiomysql 0.0.20
  • \n
\n

结果:

\n

在此输入图像描述

\n

Ser*_*rov 0

你需要承诺。await conn.commit()即使只进行查询也可以运行SELECT