2018-04-22T04:08:06Z

Flask Webcast #2: Request and Application Contexts

In this live webcast I explain what is the purpose of the request and application contexts in Flask, and why sometimes you get these weird errors about them.

8 comments

  • #1 fbu said 2018-04-28T18:30:19Z

    Hi Miguel,

    Thanks a million for this video, much appreciated!

    One question: the contexts seem to be looked up via Werkzeug utilities. Is that still true if we serve our app through a different server as eg gunicorn?

    Thanks for your help and best wishes Felix

  • #2 Miguel Grinberg said 2018-04-29T16:26:56Z

    @fbu: Yes. The support for the context implementation is in Werkzeug, but it is separate from the Werkzeug web server and works regardless of the web server that you use.

  • #3 pwabbo said 2020-08-15T16:34:06Z

    Hi, is it possible that the video https://www.youtube.com/watch?v=Z4X5Oddhcc8 has disappeared?

    Youtube seems to be giving a HTTP Error 410: Gone

    Regards.

  • #4 Miguel Grinberg said 2020-08-19T22:29:47Z

    @pwabbo: I have reported it to youtube several days ago, but I'm still waiting for it to be restored. Sorry about that, I depend on YouTube to fix this.

  • #5 Eduard said 2020-10-07T02:44:32Z

    Hi Miguel, Can you please explain how to access the application context using an init.py and main.py with a blueprint? I am running BackgroundScheduler to run a DB job at a specific time, but cannot figure out how to connect to DB inside the thread. Thanks, Eduard

  • #6 Miguel Grinberg said 2020-10-07T08:54:20Z

    @Eduard: background jobs have nothing to do with blueprints, so you would be using the same instructions I show in this video to create an app context in your background thread.

  • #7 Eduard said 2020-10-07T14:00:48Z

    Miguel, thanks for the quick response. Unfortunately, I am new to Python and I am not sure that I understand. I declare and start my background thread in main.py file where is only the blueprint, but I do not have access to the app object, and when I import the current_app object it does not work. I guess I understand the concept, I just do not know how to practically do it, and I did not see it explicitly done in your video. Thanks, Eduard

  • #8 Miguel Grinberg said 2020-10-07T18:35:33Z

    @Eduard: The current_app variable does not know the application instance on its own, someone needed to have pushed a context before, as I show in the video. You are using this variable with nobody pushing a context ahead of its use, and that is why you get the error.

    You need to figure out a way to pass the app instance to your thread. It basically comes down to that. Either move your thread code to a place where you have access to the app instance, or add a mechanism by which the app instance can be given to the thread once it is known.

Leave a Comment