Answer the question
In order to leave comments, you need to log in
How to access the current application context from a new thread?
There is a class that accepts an object that needs to be added for deletion to the sqlalchemy session.
If you implement this class within the current thread, no problems arise:
from ..extensions import db
# db = SQLAlchemy()
class Remover(object):
"""docstring for Remover"""
def __init__(self, obj):
super(Remover, self).__init__()
self.obj = obj
self.remove()
def remove(self):
try:
db.session.delete(self.obj)
db.session.commit()
except Exception, e:
raise e
u = User().query.first()
Remover(u)
from threading import Timer
from ..extensions import db
# db = SQLAlchemy()
class Remover(object):
"""docstring for Remover"""
def __init__(self, obj):
super(Remover, self).__init__()
self.obj = obj
self.t = Timer(7, self.remove)
self.t.start()
def remove(self):
try:
db.session.delete(self.obj)
db.session.commit()
except Exception, e:
raise e
def cancel(self):
self.t.cancel()
u = User().query.first()
Remover(u)
RuntimeError: application not registered on db instance and no application bound to current context
Answer the question
In order to leave comments, you need to log in
If you need to implement deferred deletion, then this is done differently and without threads.
In your case, you need to pass to Remover not a reference to an object, but a user id.
Then, in the Remover, open a new connection to the database and delete the user with the passed id.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question