In fact, this is a somewhat complicated question. If you want to fully explain the real advantages and difficulties, because I am at work, I have to briefly explain it to the subject first:
First of all, you need to understand what context is. In fact, the so-called context is the location where the required resources (variables/functions...) are saved.
Then the request context isThe life cycle is a request period, which stores a request information. For example,flask.requestis a typical request context, which is created when a request comes in and is destroyed after the request is completed.
Application context actually only existed after version 0.9. It appeared because before version 0.9, resources (such as functions) regardless of whether they were related to the request context were thrown into the request context. This is obviously unscientific, because What if there is no request context when writing UT? So a new application context was added. Its life cycle is uncertain and should be divided according to scenarios:
When providing web request service, the life cycle is from the first request coming in to the server shutting down.
When usingwith flask.AppContext(), the life cycle is the period of the with statement.
If you want to know more in-depth knowledge, you can take a look at the official documentation:
In fact, this is a somewhat complicated question. If you want to fully explain the real advantages and difficulties, because I am at work, I have to briefly explain it to the subject first:
First of all, you need to understand what context is. In fact, the so-called context is the location where the required resources (variables/functions...) are saved.
Then the request context isThe life cycle is a request period, which stores a request information. For example,
flask.request
is a typical request context, which is created when a request comes in and is destroyed after the request is completed.Application context actually only existed after version 0.9. It appeared because before version 0.9, resources (such as functions) regardless of whether they were related to the request context were thrown into the request context. This is obviously unscientific, because What if there is no request context when writing UT? So a new application context was added. Its life cycle is uncertain and should be divided according to scenarios:
When providing web request service, the life cycle is from the first request coming in to the server shutting down.
When using
with flask.AppContext()
, the life cycle is the period of the with statement.If you want to know more in-depth knowledge, you can take a look at the official documentation:
Flask Application Context Introduction
Flask Request Context Introduction