asyncpg icon indicating copy to clipboard operation
asyncpg copied to clipboard

notice callback not awaited

Open ale-dd opened this issue 3 years ago • 1 comments

  • asyncpg version: 0.23.0
  • Python version: 3.9.2
  • Platform: Linux
  • Do you use pgbouncer?: no
  • Did you install asyncpg with pip?: yes
  • Can the issue be reproduced under both asyncio and uvloop?: only experienced it once, without uvloop
/root/.local/lib/python3.9/site-packages/asyncpg/connection.py:1435: RuntimeWarning: coroutine 'vacuum_job.<locals>.async_notice_callb' was never awaited
  cb(con_ref, message)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

ale-dd avatar Jan 15 '22 02:01 ale-dd

import asyncio
import asyncpg

async def vacuum_job():
    conn = await asyncpg.connect(
        user='user', password='password', database='database', host='host')
    
    # Execute some queries or tasks
    await conn.execute('VACUUM FULL')
    
    await conn.close()

async def async_notice_callback(con, message):
    # Handle notices asynchronously here
    pass

async def main():
    conn = await asyncpg.connect(
        user='user', password='password', database='database', host='host',
        command_timeout=60,  # Adjust the timeout as needed
        notice_callback=async_notice_callback  # Optional, if you need a notice callback
    )

    # Create a task for the vacuum job
    vacuum_task = asyncio.create_task(vacuum_job())

    try:
        # Execute some queries or tasks on the main connection
        result = await conn.fetch('SELECT * FROM some_table')
        print(result)
    except asyncio.CancelledError:
        # Handle the task cancellation if needed
        pass
    finally:
        await conn.close()
        await vacuum_task  # Wait for the vacuum job to complete

if __name__ == "__main__":
    asyncio.run(main())

ljluestc avatar Dec 17 '23 03:12 ljluestc