Flask Webcast #2: Request and Application Contexts

Posted by
on under

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.

Become a Patron!

Hello, and thank you for visiting my blog! If you enjoyed this article, please consider supporting my work on this blog on Patreon!

9 comments
  • #1 fbu said

    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

    @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

    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

    @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

    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

    @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

    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

    @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.

  • #9 Anthony Rogers said

    Thanks Miguel. This was really helpful. The bit at the end that discussed the different extension initialization methods and the implications of each is really good to know.

Leave a Comment