[Python] Can't close psycopg ConnectionPool

Discussão em 'Python' iniciado por Stack, Maio 31, 2025.

  1. Stack

    Stack Membro Participativo

    I tried this example from the psycopg documentation:

    from psycopg import conninfo
    from psycopg_pool import ConnectionPool

    from config import DB_HOST, DB_NAME, DB_PASSWORD, DB_PORT, DB_USER


    with ConnectionPool(
    conninfo=conninfo.make_conninfo(
    dbname=DB_NAME,
    user=DB_USER,
    password=DB_PASSWORD,
    host=DB_HOST,
    port=DB_PORT,
    )
    ) as pool:
    with pool.connection() as conn:
    conn.execute("SELECT 1")

    with conn.cursor() as cur:
    cur.execute("SELECT 1")


    This code executes immediately without any errors but the script keeps running for a while and prints the following warnings:

    couldn't stop thread 'pool-1-worker-0' within 5.0 seconds
    hint: you can try to call 'close()' explicitly or to use the pool as context manager
    couldn't stop thread 'pool-1-worker-1' within 5.0 seconds
    hint: you can try to call 'close()' explicitly or to use the pool as context manager
    couldn't stop thread 'pool-1-worker-2' within 5.0 seconds
    hint: you can try to call 'close()' explicitly or to use the pool as context manager
    couldn't stop thread 'pool-1-scheduler' within 5.0 seconds
    hint: you can try to call 'close()' explicitly or to use the pool as context manager


    And finally finishes after showing this warning for the 4th time. I am using the pool as context manager, I also tried adding pool.close() at the end but with no different outcome. What am I doing wrong?

    I'm running:

    • python 3.13.3
    • psycopg 3.2.9
    • psycopg-pool 3.2.6

    Continue reading...

Compartilhe esta Página