Old Boys video --flask request context

requset sequel

Call (): Python, as long as the definition of the __call __ () method when creating the type, the type that can be invoked. As when the function is invoked calls this method in the object.
Enter __call__ method, a function of wsgi_app

Requests related data obtained in the environ request_context encapsulated object. . environ which is associated with the request data
ctx = self.request_context (environ)
encapsulating the data object associated with the request object request_context added to
_request_ctx_stack.push (self)

flask context

  1. threading.local can open up space for each thread is through the principle that uniquely identifies the thread to do. Each thread holds the data.

It relates to three classes
request context flow:

    def wsgi_app(self, environ, start_response):
        ① ctx = self.request_context(environ)
        error = None
        try:
            try:
                ctx.push()
                response = self.full_dispatch_request()
            except Exception as e:
                error = e
                response = self.handle_exception(e)
            except:
                error = sys.exc_info()[1]
                raise
            return response(environ, start_response)
        finally:
            if self.should_ignore_error(error):
                error = None
            ctx.auto_pop(error)

(1) The user request comes in, the call DEF Call () method proceeds wsgi_app function, all requests for execution of a function in wsgi_app.
(2) an object instantiated in request_context, the __init__ executed, the return value inside environ include all data related to the request
(3) at the instance of request, execution __init__.
(4) is then performed request_context object push (), push () function has _request_ctx_stack.push (self) of the self is request_context object request contains all relevant available data
(5) before beginning execution request_ctx_stack = LocalStack () to instantiate the __init LocalStack _, local instance of the object, performing the __init__,Here Insert Picture Description

Partial function

import functools

def func(a1,a2,a3):
    print(a1,a2,a3)

new_func = functools.partial(func,5,10)
new_func(77)

Object-Oriented

When an object-oriented bars have all the functions __ __ achieved, the object will perform any operation wherein a corresponding method.


class Foo(object):
    def __init__(self,num):
        self.num = num

    def __add__(self, other):
        data = self.num + other.num
        return Foo(data)

obj1 = Foo(11)
obj2 = Foo(22)

v = obj1 + obj2

Guess you like

Origin blog.csdn.net/xili2532/article/details/91564824