python-3.x aiomysql选择数据问题:未更新

qvtsj1bj  于 2023-06-25  发布在  Python
关注(0)|答案(1)|浏览(142)
version: 
    Python 3.6.9
    aiomysql: 0.0.20
    aiohttp: 3.6.2

问题:在mysql表中删除或插入数据,查询数据数小时不更新,除非web_app重启。
使用aiomysql池的代码:

# initial
  pool = await aiomysql.create_pool(
      # echo=True,
      db=conf['database'],
      user=conf['user'],
      password=conf['password'],
      host=conf['host'],
      port=conf['port'],
      minsize=conf['minsize'],
      maxsize=conf['maxsize'],
  )

  # query
  async def get_data(request)::
          cmd = 'select a,b,c from tbl where d = 0'
          # request.app['db'] == pool
          async with request.app['db'].acquire() as conn:
              async with conn.cursor() as cur:
                  await cur.execute(cmd)
                  ...

当前解决方案:当aiomysql.create_pool似乎可以解决问题时,设置pool_recycle=20。为什么?还有更好的方法吗?

hk8txs48

hk8txs481#

我也有这个问题,并将"autocommit": True添加到我的db conf中为我修复了它。

pool = await aiomysql.create_pool(
        host=...,
        user=...,
        password=...,
        db=...,
        port=...,
        # etc
        autocommit=True,
        loop=asyncio.get_running_loop())

相关问题