Flask中的上下文

# -*- coding: utf-8 -*-
# __author__ = "maple"
""""""
# 1. 手写flask的Hello World .


# 2. flask哪些第三方组件
"""
    flask-session
    wtforms
    sqlalchemy / pymysql /pymongo
    gevent-websocket
    其他: http://flask.pocoo.org/extensions/
"""

# 3. flask和其他框架最显著的区别.
"""
    请求处理方式不同.
        其他:传参
        flask:上下文管理机制
"""

# 4. threading.local对象的作用?
"""
import time
import threading
# class Foo(object):
#     pass
# obj = Foo()
obj = threading.local()
def task(num):
    # 获取当前线程的ID,为当前线程开辟一个块空存储当前线程的值.
    obj.number = num
    time.sleep(2)
    # 获取当前线程的ID,去当前线程ID对应存储空间中获取原来设置的值.
    print(obj.number)

for i in range(10):
    t = threading.Thread(target=task,args=(i,))
    t.start()

"""

# 5. flask使用了threading.local的升级版
# 5.1 threading.local本质
"""
import threading

storage = {
    7608:{'number':0},
    7612:{'number':1},
}

def task(num):
    # print(num,threading.get_ident())
    storage[threading.get_ident()] = {'number':num}
    
for i in range(10):
    t = threading.Thread(target=task,args=(i,))
    t.start()
"""
# 5.2 threading.local本质
"""

import threading

storage = {
}

class Local(object):

    def __setattr__(self, key, value):
        ident = threading.get_ident()
        if ident not in storage:
            storage[ident] = {key:value}
        else:
            storage[ident][key] = value

    def __getattr__(self, item):
        ident = threading.get_ident()
        try:
            return storage[ident][item]
        except KeyError:
            pass

obj = Local()

def task(num):
    obj.number = num
    print(obj.number)

for i in range(10):
    t = threading.Thread(target=task, args=(i,))
    t.start()
"""
# 5.3 threading.local本质
"""

import threading
try:
    from greenlet import getcurrent as get_ident
except:
    from threading import get_ident

storage = {

}

class Local(object):

    def __setattr__(self, key, value):
        ident = get_ident()
        if ident not in storage:
            storage[ident] = {key:value}
        else:
            storage[ident][key] = value

    def __getattr__(self, item):
        ident = get_ident()
        try:
            return storage[ident][item]
        except KeyError:
            pass

obj = Local()

def task(num):
    obj.number = num
    print(obj.number)

for i in range(10):
    t = threading.Thread(target=task, args=(i,))
    t.start()
"""

# 6. flask如何使用的加强版的threading.local?
"""
    描述:
        请求进来后,将请求封装成 ctx = RequestContext对象(杯子),将对象放到Local对象中(划分区域的空调上).
    本质:
        通过threading.local为每个线程的数据进行数据隔离.
"""

# 7. LocalStack对象
"""
    - 原请求
    - ctx = RequestContext(request,session)
    - 交给 LocalStack对象 push  (杨金涛)
    - threading.local对象中.
        {
            1231:{
                'stack':[ctx,]   # 目前:列表永远不可能存在2+个元素.
            },
            1211:{
                'stack':[]
            }
        }
    

"""

# 8. Flask上下文管理
"""
    - 请求上下文管理: ctx = RequestContext(request,session)
        ctx给 杨金涛LocalStack对象, 杨金涛把ctx放到空调上(Local对象).
        {
            1231:[
                stack: [ctx,]
            ]
        }
        日后想要获取 LocalProxy对象(阿段request)或 LocalProxy对象(郝旭session)
    
    - 应用上下文管理: app_ctx = AppContext(app,g)
        app_ctx给 冯坤LocalStack对象, 杨金涛把app_ctx放到空调上(Local对象).
        {
            1231:[
                stack:[app_ctx,]
            ]
        }
        日后想要获取 LocalProxy对象(蔡淞app)或 LocalProxy对象(郭鑫g)
"""
"""
from flask import Flask,request,session,current_app,g

app = Flask(__name__)

@app.route('/index')
def index():
    print(request.method)
    return 'index'

if __name__ == '__main__':
    app.run()
    # 一单有请求到来,就会执行app.__call__
    app.__call__
"""

# 9. LocalStack对象为什么要互为成一个栈?
"""
    在web运行时中(web runtime),无意义.
    离线脚本,有意义 + 多app
"""

# 10. flask中g的作用?以及g和全部变量的区别.
"""
    在一次请求周期中为用户维护一个值.
"""

 

posted on 2019-01-08 16:59  liangliang123456  阅读(109)  评论(0)    收藏  举报

导航