We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
The simplest way is to define conn and cursor in the initialization method, but it fails because the init method cannot be asynchronous
无
I only want to connect to the database once for a FastAPI service, rather than having to connect to the database every time a request is made
$ python --version python = 3.9
$ python -m pip show aiomysql 0.2.0
$ python -m pip show PyMySQL 1.1.0
$ python -m pip show sqlalchemy
linux
SELECT VERSION(); 无
No response
The text was updated successfully, but these errors were encountered:
i am curious too
Sorry, something went wrong.
You can use a lifespan, something like
@asynccontextmanager async def lifespan(app: FastAPI): app.state.db_engine = await create_my_async_engine() yield
And then you can define some function
def get_engine(request: Request) -> sqlalchemy.ext.asyncio.AsyncEngine: return request.app.state.db_engine
And in your routes you can access your engine like
@userRouter.get("/{user_id}") async def get_user(user_id: int, engine: sqlalchemy.ext.asyncio.AsyncEngine = Depends(get_engine)) async with engine.begin() as conn: result = await conn.execute(users.select().where(users.c.id == user_id)) return result.first()
No branches or pull requests
Describe the bug
The simplest way is to define conn and cursor in the initialization method, but it fails because the init method cannot be asynchronous
To Reproduce
无
Expected behavior
I only want to connect to the database once for a FastAPI service, rather than having to connect to the database every time a request is made
Logs/tracebacks
Python Version
aiomysql Version
PyMySQL Version
SQLAlchemy Version
$ python -m pip show sqlalchemy
OS
linux
Database type and version
Additional context
No response
Code of Conduct
The text was updated successfully, but these errors were encountered: