librelist archives

« back to archive

Question on Application Context.

Question on Application Context.

From:
Byungchan An
Date:
2014-04-12 @ 17:36
Hello, I'm novice for flask.
In official site, I read the Application context but I can't understand in
some part.

In the page (http://flask.pocoo.org/docs/appcontext/)...

*Since one of the pillar's of Flask's design is that you can have *
*more than one application in the same Python process.*

In here, what does it mean by more than one application in the same Python
process?
If possible can anyone give example or example codes for this?

Also, according to the page, application context can be created for each
request.
Does it mean each request will be treated as one application?

Thanks.

Re: [flask] Question on Application Context.

From:
Armin Ronacher
Date:
2014-04-17 @ 11:18
Hi,

On 12/04/2014 19:36, Byungchan An wrote:
> In here, what does it mean by more than one application in the same 
Python process?
> If possible can anyone give example or example codes for this?
It is entirely possible, yes.  Simple example:

     from flask import Flask

     def create_app():
         app = Flask(__name__)
         @app.route('/')
         def index():
             return 'Hello World!'
         return app

     app1 = create_app()
     app2 = create_app()

> Also, according to the page, application context can be created for each
request.
> Does it mean each request will be treated as one application?
Each request does something like this:

     with app.app_context():
         # request handling code goes here

To switch to another application you can just make an application context 
for that:

     with other_app.app_context():
         # all code here will now run below other_app instead of app.
     # at this point the previous app reactivates.


Regards,
Armin