python 异步 sqlalchemy 会话获取表

问题描述 投票:0回答:1

如何使用异步会话获取表对象列表? 我尝试了很多选择,但从未找到合适的。

这就是我获取会话对象本身的方式

from sqlalchemy.ext.asyncio import AsyncSession, create_async_engine
from sqlalchemy.orm import sessionmaker
from sqlalchemy import MetaData
from sqlalchemy import inspect
from sqlalchemy.future import select
from utils import *
import asyncio

engine = create_async_engine(config.database_url).execution_options(autocommit=True)

async_session = sessionmaker(
    engine, expire_on_commit=False, class_=AsyncSession
)

async def execute(*args):
    return await (async_session()).execute(*args)

async def tables():
    session= async_session()
    
    #print(await session.run_sync( inspect(engine).get_table_names ))

asyncio.run(tables())
python asynchronous sqlalchemy
1个回答
0
投票
async with engine.connect() as conn:
    tables = await conn.run_sync(
        lambda sync_conn: inspect(sync_conn).get_table_names()
    )
    print(tables)
© www.soinside.com 2019 - 2024. All rights reserved.