python - 程序上下文与请求上下文的区别?
巴扎黑
巴扎黑 2017-04-18 09:16:59
0
1
328

《Flask web development》表2-1将Flask上下文全局变量。
其中4种变量又分为了程序上下文和请求上下文,想问能通俗的解释下:什么是程序上下文?什么是请求上下文?两者具体区别是什么?

巴扎黑
巴扎黑

reply all (1)
左手右手慢动作

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:

  • Flask Application Context Introduction

  • Flask Request Context Introduction

    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template
    About us Disclaimer Sitemap
    php.cn:Public welfare online PHP training,Help PHP learners grow quickly!