1. Partial function
from functools import partial def func(*args,**kwargs): a=args b=kwargs return a,b new_func=partial(func,1,2,3,4,a=3,b=5) ret=new_func() print(ret)
The final results are as follows: (1, 2, 3, 4), {a': 3, `b': 5}
What I understand is that 1,2,3,4,a=3,b=5 are passed into func to execute functions.
In fact, it is: pass a value to the function without execution, return a new function.
2. Threading Local Thread Safe Space Turn Time
The thread concurrency we learned earlier is like this
from threading import Thread import threading
class foo(object): pass f=foo() def func(a): f.num=a time.sleep(1) print(f.num,threading.current_thread().ident) for i in range(10): t=Thread(target=func,args=(i,)) t.start()
But then we print the result as follows
The reason is that thread creation speed is very fast (instantaneous). When the first a enters function execution, sleep for one second, when io switches to the next, sleep again, until the last entry function, the first a is still sleeping. When the first end of sleep, num has become the last value, so each num becomes the last number 9.
Solution: (Just inherit a local in the class)
from threading import local import threading class Foo(local): pass f=Foo() def func(i): f.num=i time.sleep(1) print(f.num,threading.current_thread().ident) for i in range(10): t=Thread(target=func,args=(i,)) t.start()
3.myLocalStack
import time from threading import Thread,local import threading class MyLocalStack(local): stack={} pass mls=MyLocalStack() def func(i): a=threading.current_thread().ident mls.stack[a]=[f'r{i+1}',f's{i+1}'] time.sleep(1) print(mls.stack[a].pop(),mls.stack[a].pop(),a) mls.stack.pop(a) print(mls.stack,a) if __name__ == '__main__': for i in range(10): t=Thread(target=func,args=(i,)) t.start()
4.RunFlask + request
View function: Point to a function according to a routing address. This function is called view function.
Principle of app.run(): _____________
app.run() actually executes the run_simple method in app.
The run_simple method calls the inner _call_() method, and a wsgi_app() method is returned in _call_.
The parameters needed in the run_simple method are (host,port,func,**options)
Parameters:
host: The ip address of the server
Port: Server port
Func: Execute func functions when requests come
options: Pass-on parameters
The specific example code is as follows:
from werkzeug.serving import run_simple
from werkzeug.wrappers import Response,Request
@Request.application
def app(req):
if req.path == '/login':
return login(req)
return Response('200 OK')
def login(res):
return Response('Welcome to the landing page')
run_simple('127.0.0.1',8888,app)
req means request. It's the use in werkzeug. Just remember it.
5. Request above
How do you put app,request,session in?
First, let's be clear that the run method actually executes the _call_ method, and through the source code we find that _call_ actually returns a wsgi_app method.
Return to wsgi_app
We get that ctx returns a request_context object with - > app, request, session
The final result: top is the top method in LocalStack
Here we review the previous learning:
class Foo(object): def __call__(self, *args, **kwargs): print("I am executable") def __setattr__(self, key, value): print(key,value) def __getattr__(self, item): print(item) f=Foo()
#Object parentheses : Which to call__call__Method
f.num #What we're actually doing here is the _getattr_ method. If we don't write this function on it, we'll get an error. #The result of the above printing is num f.num=1 #The _setattr_ method is actually executed here #The results of the above printing are num, 1
So we found local.stack[-1] above, so are we looking for local or is there getattr method?
So the top result we get is None.
The final returned rv:
At this point, the request is over.
6. Request below
Request below is how you call app,request,session
The request below is only started when you execute the view function
The request below is when the function is executed, the request. method or session is used to request the content below.
Because of our previous request.method, we executed the _getattr_ method