Skip to content
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

Issue with sqlalchemy (cannot join current thread and working outside of app context) #240

Open
stautonico opened this issue Dec 7, 2023 · 4 comments

Comments

@stautonico
Copy link

Hi, I'm trying to create a background job that interacts with my database via flask-sqlalchemy and am having some trouble. Here is some (simplified) code that I'm trying to run:

# scheduler.py
from flask_apscheduler import APScheduler
scheduler = APScheduler()

# app.py
from myapp.scheduler import scheduler

app = Flask(__name__)
# ... setup stuff
scheduler.init_app(app)

# more setup stuff
configure_scheduler(app)



def configure_scheduler(app):
    def delete_expired_shares():
        with app.app_context():
            shares = Shares.query.filter(Shares.expires <= dt.now())

    scheduler.add_job(func=delete_expired_shares, trigger="interval", seconds=5, id="delete-expires-shares")
    scheduler.start()

When I include the with app.app_context(), I get the error: RuntimeError: cannot join current thread, but if I remove the with app.app_context(), it obv doesn't work (Working outside of application context). Is there something I missed in the docs? I looked at the examples and I think I set everything up correctly.

@jneeee
Copy link

jneeee commented Dec 11, 2023

Hi, Check if sqlalchemy have a config like check_same_thread=False?

        self.connection = sqlite3.connect(dbpath, check_same_thread=False)

@stautonico
Copy link
Author

I'm not quite sure what happened, but it seems to work now? I literally changed nothing. I just haven't touched the project since the 7th. I remember having to use check_same_thread in another unrelated program. I set the option and it still continues to work. I'm not sure what to think of this, but ig this is resolved?

@stautonico
Copy link
Author

stautonico commented Dec 11, 2023

I think I found the source of my issue. I tried running a job that does a db.session.commit(), and that seems to fail at the end of the job. I put a print statement at the very end, which prints right before the program throws an exception. I tried setting SQLALCHEMY_CHECK_SAME_THREAD in my app.config as well as setting ?check_same_thread=False at the end of my sqlite URI, none of which solved my issue.

@stautonico stautonico reopened this Dec 11, 2023
@stautonico
Copy link
Author

I noticed that when I comment out my @app.teardown_appcontext, it seems to work. I think I might be using teardown appcontext wrong:

@app.teardown_appcontext
def stop_scheduler(exception=None):
    scheduler.shutdown()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants