Skip to content

bind db engine and redis client to app for global usage #1742

@pessimo

Description

@pessimo

I've been using flask and tornado for some time, and I used to create a DB engine and Redis client and something like the fileserver, then bind them to app.config in the main app.py file.

# in the main app,py file
from sqlalchemy import create_engine
from redis import Redis

dbe = create_engine(...)
app.config['dbe'] = dbe

redis_cli = Redis(...)
app.config['redis_cli'] = redis_cli

then I can from flask import current_app as app and use the app.config['dbe'] to execute sqls.

# in the app view file
from flask import current_app as app
app.config['dbe'].execute(sql, **kwargs)
app.config['redis_cli'].get(...)

With this method, I don't need to initialize the DB engine and Redis client every time, and all the pre-initialized DB engine can be used in the app views(i just treat them as handlers or routers)

I'm wondering is there an equivalent method in fastAPI? Or, I need to create them every time?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions