Unzip Yearly Subscription_@mapkey注解

Unzip Yearly Subscription_@mapkey注解原文:http://werkzeug.pocoo.org/docs/0.12/local/#werkzeug.local.LocalProxySoonerorlateryouhavesomethingsyouwanttohaveineverysinglevieworhelperfunctionorwhatever.InPHPthewayt

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

原文:http://werkzeug.pocoo.org/docs/0.12/local/#werkzeug.local.LocalProxy

Sooner or later you have some things you want to have in every single view or helper function or whatever. In PHP the way to go are global variables. However, that isn’t possible in WSGI applications without a major drawback: As soon as you operate on the global namespace your application isn’t thread-safe any longer.

早晚有一天,你会想把一些东西储存在视图、辅助函数等等。在PHP中,你可以用全局变量的方式来实现。然而,由于一个主要的缺点,在WSGI应用中是不可能的:当您在全局命名空间上进行操作时,您的应用程序线程将不再安全。

The Python standard library has a concept called “thread locals” (or thread-local data). A thread local is a global object in which you can put stuff in and get back later in a thread-safe and thread-specific way. That means that whenever you set or get a value on a thread local object, the thread local object checks in which thread you are and retrieves the value corresponding to your thread (if one exists). So, you won’t accidentally get another thread’s data.


Python标准库有一个名为“本地线程”的概念(或本地线程数据)。本地线程是一个全局对象,您可以用特定、安全的线程形式将其放入并于稍后将其返回。这意味着当您给本地线程对象上设置或获取值时,本地线程对象检查您所在的线程,并检索与您的线程对应的值(如果存在)。所以,你不会不小心得到另一个线程的数据。


This approach, however, has a few disadvantages. For example, besides threads, there are other types of concurrency in Python. A very popular one is greenlets. Also, whether every request gets its own thread is not guaranteed in WSGI. It could be that a request is reusing a thread from a previous request, and hence data is left over in the thread local object.
然而,这种方法有一些缺点。例如,除了线程之外,Python中还有其他类型的并发。其中一个非常受欢迎的是greenlets。同样,在WSGI中,每个请求都有自己的线程是无法保证的。有可能该请求正在重用以前请求的线程,因此在本地线程对象中保留了数据。


Werkzeug provides its own implementation of local data storage called werkzeug.local. This approach provides a similar functionality to thread locals but also works with greenlets.

Werkzeug提供了自己的本地数据存储的实现,称为Werkzeug . local。这种方法提供了一种类似于本地线程的功能,但也适用于greenlets。

Here’s a simple example of how one could use werkzeug.local:

from werkzeug.local import Local, LocalManager

local = Local()
local_manager = LocalManager([local])

def application(environ, start_response):
    local.request = request = Request(environ)
    ...

application = local_manager.make_middleware(application)

This binds the request to local.request. Every other piece of code executed after this assignment in the same context can safely access local.request and will get the same request object. Themake_middleware method on the local manager ensures that all references to the local objects are cleared up after the request.

这将请求绑定到local. request。在同一环境下执行此任务后执行的所有其他代码都可以安全地访问本地,并且将得到相同的请求对象。本地管理器上的make_middleware方法确保在请求之后,所有引用本地对象的内容都会被清除。

The same context means the same greenlet (if you’re using greenlets) in the same thread and same process.

在相同的线程和协程中,相同的环境意味着相同的greenlet(如果您正在使用greenlets)。

If a request object is not yet set on the local object and you try to access it, you will get anAttributeError. You can use getattr to avoid that:

如果一个请求对象还没有设置在本地对象上,并且您尝试访问它,那么您将得到一个AttributeError。你可以使用getattr来避免:

def get_request():
    return getattr(local, 'request', None)

This will try to get the request or return None if the request is not (yet?) available.

这将尝试获得请求,如果请求不存在则返回None。

Note that local objects cannot manage themselves, for that you need a local manager. You can pass a local manager multiple locals or add additionals later by appending them tomanager.locals and every time the manager cleans up it will clean up all the data left in the locals for this context.

注意,本地对象不能管理自己,因此您需要一个本地管理器。您可以把多个区域变量传递给本地管理器,或在稍后通过appending的方式把额外的东西添加到管理器。每当管理器清空,会把这个环境下的所有区域变量的数据清空。

werkzeug.local.
release_local
(
local
)

Releases the contents of the local for the current context.This makes it possible to use locals without a manager.

为当前环境释放本地内容。这使得在没有管理器的情况下使用区域变量成为可能。

Example:

>>> loc = Local()
>>> loc.foo = 42
>>> release_local(loc)
>>> hasattr(loc, 'foo')
False

With this function one can release Local objects as well as LocalStack objects. However it is not possible to release data held by proxies that way, one always has to retain a reference to the underlying local object in order to be able to release it.

使用这个函数,可以释放本地对象和LocalStack对象。但是,不能通过这种方式释放代理所持有的数据,因此,始终必须保留对底层本地对象的引用,以便能够释放它。

New in version 0.6.1.

class
werkzeug.local.
LocalManager
(
locals=None,
ident_func=None
)

Local objects cannot manage themselves. For that you need a local manager. You can pass a local manager multiple locals or add them later by appending them tomanager.locals. Every time the manager cleans up,it will clean up all the data left in the locals for this context.

本地对象不能管理自己,因此您需要一个本地管理器。您可以把多个区域变量传递给本地管理器,或在稍后通过appending的方式把额外的东西添加到管理器。每当管理器清空,会把这个环境下的所有区域变量的数据清空。

The ident_func parameter can be added to override the default ident function for the wrapped locals.

可以添加ident_func参数来重写包装好的本地函数中的默认ident函数。

cleanup
(
)

Manually clean up the data in the locals for this context. Call this at the end of the request or usemake_middleware().

手动清除当前环境下的区域变量数据。在请求的最后调用这个或者使用make_middleware()。

    get_ident
(
)

             Return the context identifier the local objects use internally for this context. You cannot override this method to change the behavior but use it to link other                      context local objects (such as SQLAlchemy’s scoped sessions) to the Werkzeug locals.

                         
返回本地对象在当前环境内存下使用的环境标识符。您不能重写这个方法来改变这个行为,而是用它来连接其他环境下的本地对象(例如           SQLAlchemy                            的scoped sessions)和Werkzeug locals。

make_middleware
(
app
)

Wrap a WSGI application so that cleaning up happens afterrequest end.

封装一个WSGI应用程序,以便在请求结束后进行清理。

    middleware
(
func
)

          Like make_middleware but for decorating functions.

          make_middleware的装饰函数形式。

          Example usage:

 @manager.middleware
 def application(environ, start_response):
        ...

          The difference to
make_middleware is that the function passed will have all the arguments copied from the inner application(name, docstring, module).

          与make_middleware不同的是,传递的函数将包含从内部应用程序复制的所有参数(name、docstring、module)。

class
werkzeug.local.
LocalStack

This class works similar to a Local but keeps a stackof objects instead. This is best explained with an example:

>>> ls = LocalStack()
>>> ls.push(42)
>>> ls.top
42
>>> ls.push(23)
>>> ls.top
23
>>> ls.pop()
23
>>> ls.top
42

They can be force released by using a LocalManager or with the release_local() function but the correct way is to pop the item from the stack after using. When the stack is empty it will no longer be bound to the current context (and as such released).

可以使用LocalManager或release_local()函数释放它们,但正确的方法是使用后从堆栈中弹出条目。当堆栈为空时,它将不再被绑定到当前环境(以及类似的释放)。

By calling the stack without arguments it returns a proxy that resolves to the topmost item on the stack.

通过不带参数的方式调用堆栈,它返回一个代理,该代理解析为堆栈上的最顶层项。

pop
(
)

Removes the topmost item from the stack, will return theold value or None if the stack was already empty.

从堆栈中删除最顶层项,如果堆栈已经是空的,则返回None。

push
(
obj
)

Pushes a new item to the stack

往栈中放入一个新的项。

top

The topmost item on the stack. If the stack is empty,None is returned.

在堆栈上的最上面的项。如果堆栈是空的,则返回None。

class
werkzeug.local.
LocalProxy
(
local,
name=None
)

Acts as a proxy for a werkzeug local. Forwards all operations to a proxied object. The only operations not supported for forwarding are right handed operands and any kind of assignment.

作为一个werkzeug本地的代理。将所有操作转发到一个代理对象。唯一不支持转发的操作是向右传递的操作和任何类型的任务等。

Example usage:

from werkzeug.local import Local
l = Local()

# these are proxies
request = l('request')
user = l('user')


from werkzeug.local import LocalStack
_response_local = LocalStack()

# this is a proxy
response = _response_local()

Whenever something is bound to l.user / l.request the proxy objects will forward all operations. If no object is bound a RuntimeError will be raised.

每当有东西通过代理对象转发操作绑定到l.user/l.request。如果没有对象被绑定,则会引发运行错误。

To create proxies to Local or LocalStack objects,call the object as shown above. If you want to have a proxy to an object looked up by a function, you can (as of Werkzeug 0.6.1) pass a function to the LocalProxy constructor:

要为本地或本地堆栈对象创建代理,请调用上面所示的对象。如果您想要一个函数的代理,您可以(如Werkzeug 0.6.1)将一个函数传递给LocalProxy构造函数:

session = LocalProxy(lambda: get_current_request().session)

Keep in mind that
repr() is also forwarded, so if you want to find out if you are dealing with a proxy you can do an
isinstance() check:

请记住repr()也被转发了,所以如果您想知道您是否在处理代理,您可以执行isinstance()检查:

>>> from werkzeug.local import LocalProxy
>>> isinstance(request, LocalProxy)
True

You can also create proxy objects by hand:

你也可以手动生成代理对象:

from werkzeug.local import Local, LocalProxy
local = Local()
request = LocalProxy(local, 'request')

_get_current_object
(
)

Return the current object. This is useful if you want the real object behind the proxy at a time for performance reasons or because you want to pass the object into a different context.

返回当前对象。如果您想要在代理的背后支持真实对象,或者因为您想要将对象传递到不同的上下文中,这将非常有用。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/189512.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 详解单调队列算法

    详解单调队列算法前言如果你对这篇文章可感兴趣,可以点击「【访客必读-指引页】一文囊括主页内所有高质量博客」,查看完整博客分类与对应链接。在上一篇文章中,我们介绍了「单调栈」这一最常考察的线性数据结构。而今天我们将继续沿着这个思路,介绍另一个与其“齐名”的线性数据结构,即「单调队列」。「单调队列」在「数据结构」题中的分布较为广泛,且常被当作优化「动态规划」的一种重要手段,因此该算法在面试中考察的频率较高,属于必知必会的知识点。队列首先我们来回忆一下「队列」。「队列」是一种「先进先出」的线性数据结构,其中元素

    2022年6月25日
    21
  • 内部服务器500错误原因解决方法_什么是内部服务器错误

    内部服务器500错误原因解决方法_什么是内部服务器错误http500内部服务器错误的解决方法这个错误整整浪费了我下午的时间,在网上有很多的方法,当然我也是从那些繁多的方法中一点点的搞定IIS的,首先你要先装好IIS,XPSP2中的应该是5.1版本的,安装方法:1->打开控制面板,选择添加删除程序2->选择添加删除组件,选择Internet信息服务,也就是IIS3->点击下一步安装就好了安装好之后也许你的机子会正常的显示http://localho

    2022年8月11日
    11
  • 墙壁rj45插线顺序_网线插座接法详细图解

    墙壁rj45插线顺序_网线插座接法详细图解  RJ45接口默认有2种线序,分别如下:普通线序12345678A序白-绿绿白-橙蓝白-蓝橙白-棕棕B序白-橙橙白-绿蓝白-蓝绿白-棕棕  一般,采用B序来设置RJ45插座,即白橙橙,白绿蓝,白蓝绿,白棕棕,线序为123454678。图(1)RJ45网线的A序、B序  有些网口插座内置了桥接模块,线与线之间有对调,它不是按照从小到大排序,而是打乱的一种线序。1、CAT5ET568A/B  

    2025年12月14日
    3
  • ubuntu中文社区_linux中文系统

    ubuntu中文社区_linux中文系统“新氧ubuntu中文定制版”是由新氧ubuntu中文定制版项目组在近期推出的一个基于Ubuntu的中文Linux发行版。根据其描述:新氧ubuntu中文定制版,是基于ubuntu官方发布版制作的中文优化应用版本。它不是一个新的发布版。新氧ubuntu中文定制版是一个非盈利项目,因为其包含了一些独特的适用于中文用户的组件,一经推出就受到了广泛的关注。新氧ubuntu…

    2025年8月25日
    5
  • 计算机二级考试数据结构与算法知识点_算法与数据结构是计算机两大基础

    计算机二级考试数据结构与算法知识点_算法与数据结构是计算机两大基础按照自己的理解写的解题思路,如有错误希望指正。1.算法的复杂度: ①时间复杂度:执行算法所需的计算工作量(又叫:基本运算次数) ②空间复杂度:执行算法所需的内存 它们是没有任何关系的!!!2.求二叉树序列类题目 要点:前序—根左右 中序—左根右 后序—左右根 例1:已知前序ABCDE,中序BCADE,求后序;同类型,已知任意两个求第三个 解题思路: 由前序知道A是根,结合中序,CB是左子树,DE…

    2022年8月18日
    7
  • js整除取余(python)

    1.丢弃小数部分,保留整数部分parseInt(5/2)22.向上取整,有小数,则整数部分加1Math.ceil(5/2)33.四舍五入Math.round(5/2)34.向下取整Math.floor(5/2)2取余数0%401

    2022年4月15日
    63

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号