flask之中间件、蓝图、请求上下文等相关内容-142
from flask import Flask
app = Flask(__name__)
2 蓝图
1 是一个类的对象,用来划分项目目录,为了避免使用app划分目录的时候出现循环导入问题
2 使用步骤
-实例化得到一个蓝图对象
from flask import Blueprint
account = Blueprint('account', __name__)
-像使用app一样使用蓝图,注册路由
3 flask请求上下文
1 当flask启动,等待客户端请求
2 一旦请求来了:app()--->app.__call__()---->app.wsgi_app()
3 wsgi_app()源码如下:
# environ:http请求字典
# 返回一个ctx=RequestContext,对象中有Request对象,Session对象。。。
ctx = self.request_context(environ)
error = None
try:
try:
# 把ctx对象放到了全局变量_request_ctx_stack中
# _request_ctx_stack全局变量是LocalStack的对象
# LocalStack类的push方法
'''
def push(self, obj):
rv = getattr(self._local, "stack", None)
if rv is None:
self._local.stack = rv = []
rv.append(obj)
return rvbefore_first_request,before_request,after_request
'''
ctx.push()
# 执行请求扩展的东西(before_first_request,before_request,after_request)
# 根据路径执行视图函数
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 requirements.txt
1 在虚拟环境中直接导出
pip3 freeze >requirements.txt
2 系统环境装了很多模块,只导出当前项目依赖的模块
-pip3 install pipreqs
-pipreqs ./ --encoding=utf8
2 方法和函数的区别
from types import MethodType,FunctionType
class Foo(object):
def fetch(self):
pass
# print(isinstance(Foo.fetch,MethodType))
# print(isinstance(Foo.fetch,FunctionType)) # True
obj = Foo()
print(isinstance(obj.fetch,MethodType)) # True
print(isinstance(obj.fetch,FunctionType))
3 threading.local
# 不用local,会出现并发安全的问题
# from threading import Thread
# import time
# lqz = -1
# def task(arg):
# global lqz
# lqz = arg
# # time.sleep(2)
# print(lqz)
#
# for i in range(10):
# t = Thread(target=task,args=(i,))
# t.start()
# 使用local
# from threading import Thread
from threading import local
# import time
# from threading import get_ident
# # 特殊的对象,不同线程操作它,操作的是线程自己的,不会出现并发安全问题
# lqz = local()
# def task(arg):
# # 对象.val = 1/2/3/4/5
# lqz.value = arg
# time.sleep(2)
# print(lqz.value)
# for i in range(10):
# t = Thread(target=task,args=(i,))
# t.start()
# 自己写个local对象(low版本)
# from threading import get_ident,Thread
# import time
# storage = {}
# # {线程id号1:{key:value},线程id号2:{key:value},线程id号3:{key:value}}
# def set(k,v):
# ident = get_ident() # 获取线程id号
# if ident in storage:
# storage[ident][k] = v
# else:
# storage[ident] = {k:v}
# def get(k):
# ident = get_ident()
# return storage[ident][k]
# def task(arg):
# set('val',arg)
# time.sleep(2)
# v = get('val')
# print(v)
#
# for i in range(10):
# t = Thread(target=task,args=(i,))
# t.start()
# 面向对象版本
# from threading import get_ident,Thread
# import time
# class Local(object):
# storage = {}
# # {线程id号1: {key: value}, 线程id号2: {key: value}, 线程id号3: {key: value}}
# def set(self, k, v):
# ident = get_ident()
# if ident in Local.storage:
# Local.storage[ident][k] = v
# else:
# Local.storage[ident] = {k: v}
# def get(self, k):
# ident = get_ident()
# return Local.storage[ident][k]
# obj = Local()
# def task(arg):
# obj.set('val',arg)
# v = obj.get('val')
# print(v)
# for i in range(10):
# t = Thread(target=task,args=(i,))
# t.start()
# 面向对象版本,通过.取值,赋值
# from threading import get_ident,Thread
# import time
# class Local(object):
# storage = {}
# ## {线程id号1: {key: value}, 线程id号2: {key: value}, 线程id号3: {key: value}}
# def __setattr__(self, k, v):
# ident = get_ident()
# if ident in Local.storage:
# Local.storage[ident][k] = v
# else:
# Local.storage[ident] = {k: v}
# def __getattr__(self, k):
# ident = get_ident()
# return Local.storage[ident][k]
# obj = Local()
# obj2 = Local() # 所有的local对象用的是同一个字典,不好
# def task(arg):
# obj.val = arg
# print(obj.val)
# for i in range(10):
# t = Thread(target=task,args=(i,))
# t.start()
# 不同local对象,使用自己的字典
# from threading import get_ident,Thread
# import time
# class Local(object):
# def __init__(self):
# # self.storage={} 递归
# # setattr(self,'storage',{}) 递归
# #Local.__setattr__(self,'storage',{})
#
# object.__setattr__(self,'storage',{})
#