-
-
Notifications
You must be signed in to change notification settings - Fork 8.5k
Closed
Labels
Description
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_clithen 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?
mvoitko, sergei3000, baby5 and carlosporta