zoukankan      html  css  js  c++  java
  • 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个先线程,
    
    永远不要高估自己
  • 相关阅读:
    oracle数据库同步
    软件设计过程中常用的几种图(一)
    面向对象建模为基础的开发模式
    常用正则表达式
    js 创建一个浮动div
    DATALIST 绑定数据分页
    ASP.NET 3.5 Extensions CTP Preview Released
    重构是一种意识流
    重构随笔: 封装集合(Encapsulate Collection)
    乱笔.................table控件,带一列合并
  • 原文地址:https://www.cnblogs.com/liqiangwei/p/14398206.html
Copyright © 2011-2022 走看看