Skip to content

How to initialize a database connection only once when deploying services using FastAPI #986

Open
@TheHonestBob

Description

@TheHonestBob

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

$ python --version
python = 3.9

aiomysql Version

$ python -m pip show aiomysql
0.2.0

PyMySQL Version

$ python -m pip show PyMySQL
1.1.0

SQLAlchemy Version

$ python -m pip show sqlalchemy

OS

linux

Database type and version

SELECT VERSION();

Additional context

No response

Code of Conduct

  • I agree to follow the aio-libs Code of Conduct

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions