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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 提取视频中的音频——python三行程序搞定「建议收藏」

    提取视频中的音频——python三行程序搞定「建议收藏」写在开头提取音频安装python包提取音频分析音频安装python包读取音频matplotlib画信号强度图librosa画信号强度图写在开头  身处数据爆炸增长的时代,各种各样的数据都飞速增长,视频数据也不例外。我们可以使用python来提取视频中的音频,而这仅仅需要安装一个体量很小的python包,然后执行三行程序!  语音数据在数据分析领域极为重要。比如可以分析语义、口音、根据人的情绪等等。可以应用于偏好分析、谎话检测等等。提取音频  需要..

    2022年5月12日
    44
  • hdu3280Equal Sum Partitions (区间DP)「建议收藏」

    hdu3280Equal Sum Partitions (区间DP)

    2022年1月28日
    55
  • arcgis附图制作_怎么制作图片的浮雕效果

    arcgis附图制作_怎么制作图片的浮雕效果ArcGIS制作地图时可以制作出很多很炫的效果,比如地图阴影、地图晕渲效果、浮雕效果、三维效果等等。本实验讲解在ArcGIS中制作浮雕效果地图,效果如下所示:1.加载矢量数据加载实验数据包data44.rar中的秦安县乡镇矢量数据:2.缓冲区分析点击【地理处理】下拉菜单,点击【缓冲区】。输入要素选择秦安县乡镇数据,选择输出要素路径,线性单位输入-0.4,单位为千米,侧类型选择两侧,融合类型选择ALL,点击确定。缓冲区效果:3.欧氏距离分析欧氏距离工具用于计算每个像元到最近.

    2022年9月5日
    2
  • python注入_Python——dll注入

    python注入_Python——dll注入dll攻击原理分析什么是dll动态链接库,是在微软Windows操作系统中实现共享函数库概念的一种方式。这些库函数的扩展名是”.dll”、”.ocx”(包含ActiveX控制的库)或者”.drv”(旧式的系统驱动程序)。为何有dll由于进程的地址空间是独立的(保护模式),当多个进程共享相同的库时,每个库都在硬盘和进程彼此的内存存放一份的话,对于早期的计算机来说,无疑是一种极大的浪费,于是win…

    2022年5月17日
    30
  • Model_ModelMap_Map「建议收藏」

    Model_ModelMap_Map「建议收藏」Model_ModelMap_Map

    2022年6月18日
    25
  • 什么是SSH 以及常见的ssh 功能

    什么是SSH 以及常见的ssh 功能什么是SSH?简单说,SSH是一种网络协议,用于计算机之间的加密登录。如果一个用户从本地计算机,使用SSH协议登录另一台远程计算机,我们就可以认为,这种登录是安全的,即使被中途截获,密码也不会泄露。最早的时候,互联网通信都是明文通信,一旦被截获,内容就暴露无疑。1995年,芬兰学者TatuYlonen设计了SSH协议,将登录信息全部加密,成为互联网安全的一个基本解决方案,迅速在全世界获得推广,目…

    2022年7月13日
    11

发表回复

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

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