【发布时间】:2022-11-01 13:27:22
【问题描述】:
我正在使用 graphql(草莓)创建一个异步 fastAPI 微服务。我的数据库托管在 GoogleCloudSQL 上,它是一个 postgres 数据库。我的微服务在本地和本地数据库上运行得非常好,但是现在当我将我的连接器构建到 GoogleCloudSQL 时,它不再那么好用了。 我的问题是,如何为我的每个请求创建一个会话池并生成会话?
以下是一些代码sn-ps:
`
# [START cloud_sql_postgres_sqlalchemy_connect_connector]
import os
from sqlalchemy.ext.asyncio import create_async_engine, AsyncEngine
from google.cloud.sql.connector import IPTypes, create_async_connector
import asyncpg
async def connect_with_connector() -> AsyncEngine:
instance_connection_name = os.environ["INSTANCE_CONNECTION_NAME"] # e.g. 'project:region:instance'
db_user = os.environ["DB_USER"] # e.g. 'my-db-user'
db_pass = os.environ["DB_PASS"] # e.g. 'my-db-password'
db_name = os.environ["DB_NAME"] # e.g. 'my-database'
ip_type = IPTypes.PRIVATE if os.environ.get("PRIVATE_IP") else IPTypes.PUBLIC
# initialize Cloud SQL Python Connector object
connector = await create_async_connector()
async def getconn() -> asyncpg.Connection:
conn: asyncpg.Connection = await connector.connect_async(
instance_connection_name,
"asyncpg",
user=db_user,
password=db_pass,
db=db_name,
ip_type=IPTypes.PUBLIC
)
return conn
# The Cloud SQL Python Connector can be used with SQLAlchemy
# using the 'creator' argument to 'create_engine'
connection = await getconn()
pool = create_async_engine(
"postgresql+asyncpg://",
creator=connection,
# [START_EXCLUDE]
# Pool size is the maximum number of permanent connections to keep.
pool_size=5,
# Temporarily exceeds the set pool_size if no connections are available.
max_overflow=2,
# The total number of concurrent connections for your application will be
# a total of pool_size and max_overflow.
# 'pool_timeout' is the maximum number of seconds to wait when retrieving a
# new connection from the pool. After the specified amount of time, an
# exception will be thrown.
pool_timeout=30, # 30 seconds
# 'pool_recycle' is the maximum number of seconds a connection can persist.
# Connections that live longer than the specified amount of time will be
# re-established
pool_recycle=1800, # 30 minutes
# [END_EXCLUDE]
)
return pool`
这是我的会话生成器
`@asynccontextmanager
async def get_session() -> AsyncSession:
engine = await connect_with_connector()
async_session = sessionmaker(
engine, class_=AsyncSession, expire_on_commit=False
)
async with async_session() as session:
yield session`
当我尝试像这样执行我的查询时:
async with get_session() as session:
selected = await session.execute(selectable)
我收到此错误: “‘连接’对象不可调用”
即使我在调试时看到 session 的类型为 AsyncSession
【问题讨论】:
-
“它不再那么好用了” - 你能更详细地解释一下吗?
-
当我尝试执行查询时收到此错误
selected = await db.execute(selectable)“'Connection' 对象不可调用”即使在调试时我看到会话的类型为 AsyncSession -
您能否分享完整的回溯,而不仅仅是错误消息。除非我们知道错误发生在哪里,否则“'Connection' 对象不可调用”并没有多大意义。
标签: python postgresql google-cloud-platform sqlalchemy google-cloud-sql