老男孩视频——flask请求上下文

requset续集

call():Python中,只要在创建类型的时候定义了__call__()方法,这个类型就是可调用的。在对象作为函数被调用的时候会调用这个方法。
进入__call__方法,的wsgi_app函数

将请求相关得数据environ封装在request_context对象中。。environ里面就是请求相关的数据
ctx = self.request_context(environ)
将封装了请求相关的数据request_context对象中的对象,添加到了
_request_ctx_stack.push(self)

flask上下文

  1. threading.local可以给每个线程开辟空间,原理就是通过线程的唯一标识来做。每个线程都保存了数据。

涉及三个类
请求上下文流程:

    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).用户请求进来,调用 def call()方法,进入wsgi_app函数,所有的请求都在wsgi_app函数中进行执行第一句。
(2) 在request_context 实例化一个对象,执行__init__, 返回值里面有environ包括请求相关的所有数据
(3) 在request 中实例化,执行__init__ 。
(4).然后在request_context 对象中执行 push(), push()函数中有_request_ctx_stack.push(self) 中的self 是request_context 对象,包含了请求相关得所有数据
(5).最开始先执行 request_ctx_stack = LocalStack() 对于LocalStack进行实例化__init_,实例化local对象,执行__init__, 在这里插入图片描述

偏函数

import functools

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

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

面向对象

当吧面向对象中得所有__函数__实现时,对象做任何操作都会执行其中相对应的方法。


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

猜你喜欢

转载自blog.csdn.net/xili2532/article/details/91564824