flask framework - under

Local and partial function

threasing.local

  Multiple threads to modify the same data, multiple copies to each thread with a variable, open up a space to store books for each thread.

Do not use therading.local

# 不用local
from threading import Thread
import time
cxw = -1
def task(arg):
    global cxw
    cxw = arg
    # time.sleep(2)
    print(cxw)

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

 

Result Analysis: The results showed that in all 9 runtime, chaotic data, when the time.sleep delete, print the result of normal 1,2,3,4 .... 10.

Use threading.local

from threading import Thread
from threading import local
import time
from threading import get_ident
# 特殊的对象
cxw = local()
def task(arg):
    # 对象.val = 1/2/3/4/5
    cxw.value = arg
    time.sleep(2)
    print(cxw.value)
for i in range(10):
    t = Thread(target=task,args=(i,))
    t.start()

 

  Use local () when the data can be displayed properly, no chaos.

Custom threaing.local (function version) by dictionary

# Get under thread id 
from Threading Import get_ident, the Thread
 Import Time
storage = {}
def set(k,v):
    index = get_ident()
    if index in storage:
        storage[index][k] = v
    else:
        storage[index] = {k:v}
def get(k):
    index = get_ident()
    return storage[index][k]

def task(arg):
    set("val",arg)
    v = the get ( ' val ' )
     print (v)

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

 

Object-oriented version

from threading import get_ident,Thread
import time
class Local(object):
    storage = {}
    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 = the Local () # instantiate the object 
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()

 

Achieved by setattr and getattr

# Reflection 
# hasattr (Object, name) # determine whether an object has a property 
# setattr (Object, name, value) # object to add new attributes 
# getattr (Object, name, default) # Gets a property from an object 
# delattr (Object, name) # delete a property from an object
#
from threading import get_ident,Thread
import time
class Local(object):
    storage = {}
    def __setattr__(self, key, value):
        ident = get_ident()
        if ident in Local.storage:
            Local.storage[ident][key] = value
        else:
            Local.storage[ident] = {key:value}
    def __getattr__(self, key):
        ident = get_ident()
        return Local.storage[ident][key]
obj = Local()
def task(arg):
    obj.val = arg
    print(obj.val)
for i in range(10):
    t = Thread(target=task,args=(i,))
    t.start()

 

Each object has its own memory space (dictionary)

from threading import get_ident,Thread
import time
class Local(object):
    def __init__(self):
        object.__setattr__(self,'storage',{})
        self.storage={}
    def __setattr__(self, k, v):
        ident = get_ident()
        if ident in self.storage:
            self.storage[ident][k] = v
        else:
            self.storage[ident] = {k: v}
    def __getattr__(self, k):
        ident = get_ident()
        return self.storage[ident][k]
obj = Local()
def task(arg):
    obj.val = arg
    obj.xxx = arg
    print(obj.val)
for i in range(10):
    t = Thread(target=task,args=(i,))
    t.start()

 

And coroutine compatible thread (see the source code to the request, the local look getattr , setattr)

try:
    from greenlet import getcurrent as get_ident
except Exception as e:
    from threading import get_ident
from threading import Thread
import time
class Local(object):
    def __init__(self):
        object.__setattr__(self,'storage',{})
    def __setattr__(self, k, v):
        ident = get_ident()
        if ident in self.storage:
            self.storage[ident][k] = v
        else:
            self.storage[ident] = {k: v}
    def __getattr__(self, k):
        ident = get_ident()
        return self.storage[ident][k]
obj = Local()
def task(arg):
    obj.val = arg
    obj.xxx = arg
    print(obj.val)
for i in range(10):
    t = Thread(target=task,args=(i,))
    t.start()
   

 

Some Local-implemented method above to derive source code to achieve the following: Get get_ident.

Source entrance: app .__ call __ >>> wsgi _ >>> push >>> Local >>

 

partial partial function

# Partial function of the second portion (variable parameter), Parametric order to supplement the original function, the parameters acting on the original function, the function returns a new last partial function 
from functools Import partial
 DEF Test (A, B, C , D):
     return A + B + C + D

TES = partial ( Test, 1,2 ). 1 = A #, B = C = 2. 3, D =. 4
 Print (TES (3,4-)) 
must follow the transmission parameters in a unified manner, scenarios, parameter passing in the form of simple casual , do not set the default variable values.

source code analysis session

SecureCookieSessionInterface
    -open_session
    -save_session

Request context

 

 

 

 

 

 

 

 

blueprint

 

g Object

 signal

flask-session

Guess you like

Origin www.cnblogs.com/Gaimo/p/11853040.html