-
Notifications
You must be signed in to change notification settings - Fork 184
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Running job causes detachedinstanceerror #221
Comments
hey @bnewman70, so if I understand you correctly, the error is only coming up in jobs that have the |
It is difficult to follow, maybe if you have code on github somewhere it would be easier. It sounds as though you have a db session, and after you pass it into a job, the session is no longer accessible outside the job. Are you using the extensions pattern (similar to this example) to create you db? You could have something like from flask_sqlalchemy import SQLAlchemy
from flask_sqlalchemy_caching import CachingQuery
db = SQLAlchemy(query_class=CachingQuery) in your extensions.py and then import it where you need db connections, and use it inside the app_context(). In your init.py you would still need to also import |
Is there any chance that when running a job using the scheduler within an app context that it can cause a previous session to detach?
i am using version 1.12.3 of flask-apscheduler, I only get the following error when running a job that uses the database. If I run a job that just prints to screen, I do not get this error.
sqlalchemy.orm.exc.DetachedInstanceError: Parent instance <Users at 0x2792f1448b0> is not bound to a Session; lazy load operation of attribute 'profile' cannot proceed.
The call for profile is used by flask-login on @login_required decorator. I have tried many scenarios and this only seems to happen when a job runs. I am using the @scheduler.task decorator to start the job and have my code within this block
with scheduler.app.app_context():
So this works fine
@scheduler.task("interval",id=ups_tracking,seconds=60,max_instances=1,start_date="2000-01-01 12:19:00")
def hello_world():
print("hello world job")
this produces error
@scheduler.task("interval",id=ups_tracking,seconds=60,max_instances=1,start_date="2000-01-01 12:19:00")
def hello_world():
with scheduler.app.app_context():
print("hello world job")
not even doing any db queries
and this only happens when using scheduler.run_job() method on demand.
The text was updated successfully, but these errors were encountered: