Как мне настроить кеш пула aiomysq? - PullRequest
1 голос
/ 08 мая 2020

Когда я использую mysql_pool из aiomysql для обновления фрагмента данных, первое время такое же, как и во второй раз.

class Test(object):
    async def _pool(self):
        self.pool = await aiomysql.create_ç(**mysql_options)

    async def get_one(self, sql, param=None):
        await self.cur.execute(sql, param)
        result = await self.cur.fetchone()
        return result

    async def get(self):
        self.conn = await self.pool.acquire()
        self.cur = await self.conn.cursor(DictCursor)
        sql = '''select policy from tb_user where id = 2;'''
        res = await self.get_one(sql)
        print(res)
        await self.cur.close()
        await self.pool.release(self.conn)

    @staticmethod
    def update():
        import pymysql
        coon = pymysql.connect(host='127.0.0.1',
                               port=3306,
                               user=mysql_options['user'],
                               autocommit=True,
                               password=mysql_options['password'],
                               database=mysql_options['db'])
        cursor = coon.cursor()
        sql = '''update tb_user set policy = 9 where id = 2;'''
        cursor.execute(sql)
        sql = '''select policy from tb_user where id = 2;'''
        cursor.execute(sql)
        data = cursor.fetchone()
        print(data)
    async def run(self):
        await self._pool()
        await self.get()
        self.update()
        await self.get()


if __name__ == '__main__':
    test = Test()
    loop = asyncio.get_event_loop()
    loop.run_until_complete(test.run())

версия

  • python 3.7.0
  • aio mysql 0.0.20

результат

enter image description here

...