其他分享
首页 > 其他分享> > Flask之request实现思想

Flask之request实现思想

作者:互联网

Flask之全局的request,每次请求不一样的,是如何实现的???

了解flask的都知道,每次请求request的对象,是不一样的,但request是全局对象,内部怎么做到的?在我们请求的时候,uwgsi会起一个线程,然而线程之间数据是可以共享的 ,flask是这样的思想,根据线程号,假设有一个全局的大字典 global_dict = {},根据线程号,这样存储 :

 global_dict {'线程号1':{key:val,key1:val1},
              '线程号2':{key:val,key1:val1},
             '线程号3':{key:val,key1:val1},...
             }

来要数据的时候根据你的线程号来区分:

初级版本

###############1,多线程写同一个数据,会导致错乱
from threading import Thread
import time
aaa = -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()
打印出都是 9
----------------------------------------
根据线程号存放值
from threading import get_ident,Thread
import time
storage = {}
#{'线程id':{value:1},'线程id':{value:2}....}
def set(k,v):
    ident = get_ident()
    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)
    v = get('val')
    print(v)

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

flask源码思想版本:

try:
    from greenlet import getcurrent as get_ident  #将协程号的函数也as成get_ident,统一接口,
except Exception as e:
    from threading import get_ident  #获取线程号

class Local(object):
    def __init__(self):

        self.__dict__['glo_dic'] = {}     #  方法1 ,类所有的属性  方法都在这里,反射也是看__dict__里面有没有,将全局的字典放在这
        # self.__class__.__dict__['glo_dic'] = {}  一开始我想放在类里的,发现类的__dict__只能看不能放进去,你也可以在Local里直接写
        #object.__setattr__(self,'glo_dic',{}) 调用object的__detattr__

    def __setattr__(self, key, value):
        if get_ident() in self.glo_dic:   #判断线程号是否在里面
            self.glo_dic[get_ident()][key] = value
        else:
            self.glo_dic[get_ident()] = {key:value}
    def __getattr__(self, item):
        return self.glo_dic[get_ident()].get(item)

    def __getitem__(self, item):
        return self.__getattr__(item)
    def __setitem__(self, key, value):
        self.__setattr__(key,value)

a = Local()

def f1(i):
    a['val'] = i   #调用__setitem__
    time.sleep(2)
    print(a.val)   #调用__getattr__   
if __name__ == '__main__':
    li = []
    for i in range(10):
        li.append(Thread(target=f1,args=(i,)))   
    for t in li:
        t.start()  #起10个先线程,

 

标签:__,ident,思想,get,Flask,self,request,线程,def
来源: https://www.cnblogs.com/liqiangwei/p/14398206.html