MagicStack / asyncpg

A fast PostgreSQL Database Client Library for Python/asyncio.
Apache License 2.0
6.86k stars 398 forks source link

notice callback not awaited #876

Open ale-dd opened 2 years ago

ale-dd commented 2 years ago
/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
ljluestc commented 9 months ago
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())