Flask Context Management--(delivery of Session,request,current_app)

Source: Internet
Author: User

Transfer of Flask Session,request,current_app


1 Flask request, session and Current_app are set up in a relatively new way-through the context of the management of the way to achieve

每次请求进来app.run调用 call 方法, 创建 一个本地线程(唯一标识作为键) -- 然后把实例化的对象push到一个地方,在请求结束后返回的时候 pop 掉local = {        '标识':{'stack':[RequestContext(),]}        }

2 Supplemental Partial function

其实就是函数调用的时候,有多个参数 参数,但是其中的一个参数已经知道了,我们可以通过这个参数重新绑定一个新的函数,然后去调用这个新函数。from functools import partialdef f(a,b):    return a + bf1 = partial(f,10)print(f1(5))-->> 15

3 Unique identification

theading_local  -->> 每一个线程 创建一个from greenlet import getcurrent as get_ident可以基于 greenlet -->> 粒度更细        比如 wsgi  -- 有基于线程的,也有基于协程实现的本地线程:                    import threading    local_values = threading.local()    def func(num):        local_values.name = num        import time        time.sleep(1)        print(local_values.name, threading.current_thread().name)    for i in range(20):        th = threading.Thread(target=func, args=(i,), name='线程%s' % i)        th.start()

Flask Context Management--(delivery of Session,request,current_app)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.