From 8abbcb6e863deb6e0cf077d5fa780a2be5792931 Mon Sep 17 00:00:00 2001 From: chrispy Date: Sun, 4 May 2025 09:40:22 -0400 Subject: [PATCH] open AsyncConnectionPool outside constructor Signed-off-by: chrispy --- lib/sqlalchemy/dialects/postgresql/psycopg.py | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/sqlalchemy/dialects/postgresql/psycopg.py b/lib/sqlalchemy/dialects/postgresql/psycopg.py index 5c8f1030d7..5df2403992 100644 --- a/lib/sqlalchemy/dialects/postgresql/psycopg.py +++ b/lib/sqlalchemy/dialects/postgresql/psycopg.py @@ -148,17 +148,18 @@ Here is an example that uses ``psycopg_pool.AsyncConnectionPool``:: min_size=1, # Minimum pool size max_size=5, # Maximum pool size max_idle=60, # Maximum idle time (seconds) for unused pool connections + open=False, # See comment below ) - # Define a callable that awaits a connection - async def get_connection(): - return await mypool.getconn() + # Must explicitly open AsyncConnectionPool outside constructor + # (https://www.psycopg.org/psycopg3/docs/advanced/pool.html#other-ways-to-create-a-pool) + await mypool.open() # Create an engine that uses the connection pool to get a connection engine = create_async_engine( url="postgresql+psycopg://", # Only need the dialect now poolclass=NullPool, # Disable SQLAlchemy's default connection pool - async_creator=get_connection, # Use Psycopg 3 connection pool to obtain connections + async_creator=mypool.getconn, # Use Psycopg 3 connection pool to obtain connections ) The resulting engine may then be used normally. Internally, Psycopg 3 handles -- 2.47.3