@contextmanager
def connect_db_with(db_pool: ThreadedConnectionPool):
    try:
        connection = db_pool.getconn()
        connection.set_client_encoding('UTF-8')
        psycopg2.extras.register_json(connection, loads=loads)
        yield connection
    finally:
        db_pool.putconn(connection)


with connect_db_with(pool) as db:
    cursor = db.cursor()
    result = select_session_by_id(cursor, session_id)
    pool.putconn(db)
    return result

Изменить пасту