Using flask-sqlalchemy
, how is it possible to connect to a database from within a redis task?
The database connection is created in create_app
with:
db = SQLAlchemy(app)
I call a job from a route:
@app.route("/record_occurrences")
def query_library():
job = queue.enqueue(ApiQueryService(word), word)
Then inside the redis task, I want to make an update to the database
class ApiQueryService(object):
def __init__(self,word):
resp = call_api()
db.session.query(Model).filter_by(id=word.id).update({"count":resp[1]})
I can't find a way to access the db. I've tried importing it with from app import db
. I tried storing it in g
. I tried reinstantiating it with SQLAlchemy(app)
, and several other things, but none of these work. When I was using sqlite, all of this worked, and I could easily connect to the db from any module with a get_db
method that simply called sqlite3.connect()
. Is there some simple way to access it with SQLAlchemy that's similar to that?