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


相关推荐

  • navicatmysql连接不上_navicat找不到本地MySQL服务

    navicatmysql连接不上_navicat找不到本地MySQL服务最近遇到了一件非常棘手的问题,用Navicat远程连接数据库居然连接不到,真是头都大了。 网上查阅了一下原因,原来是没有开通远程权限,好吧,下面我就来回忆一下自己怎么处理这问题的!大家都知道,用Navicat连接数据库一般是这样连得:问题整理以及解决办法错误一:错误原因:本地IP(xxx.xxx.xxx.xxx)没有访问远程数据库的权限。于是下面开启本地IP(xxx.xxx.xxx.xxx)对远程…

    2022年10月9日
    4
  • 分布式通信基础

    1.分布式通信基础思维导图2.IP协议2.1ICMP协议ICMP协议(InternetControlMessageProtocol),Internet控制报文协议,其作用就是探测网络

    2021年12月28日
    48
  • 查看mysql日志命令_linux查看mysql安装路径

    查看mysql日志命令_linux查看mysql安装路径centos是linux吗_网站服务器运行维护centos是一个基于RedHatLinux提供的可自由使用源代码的企业级Linux发行版本,它是来自于RedHatEnterpriseLinux依照开放源代码规定释出的源代码所编译而成。Linux中MySQL日志在哪Linux中MySQL日志一般保存在/var/log/目录下,但还需要看具体的配置文件才能确定,具体方法如下:1、首先登陆…

    2022年10月14日
    2
  • Java和C++的区别

    Java和C++的区别Java和C++的区别:1.Java是解释型语言,所谓的解释型语言,就是源码会先经过一次编译,成为中间码,中间码再被解释器解释成机器码。对于Java而言,中间码就是字节码(.class),而解释器在JVM中内置了。2.C++是编译型语言,所谓编译型语言,就是源码一次编译,直接在编译的过程中链接了,形成了机器码。3.C++比Java执行速度快,但是Java可以利用JVM跨平台。4….

    2022年7月7日
    28
  • 作业2 分析TGA文件「建议收藏」

    一、TGA文件格式解析二、文件格式文件头(TgaFileHeader):由图像描述信息字段长度、颜色表类型、图像类型、颜色表说明和图像说明五个字段组成,总计18字节,描述了图像存储的基本信息,应用程序可依据该部分字段值读写图像数据。图像/颜色表数据(Image/ColorMapData):由图像描述信息(可选)、颜色表数据和图像数据三部分组成,用于存储图片的图像信息。开发者自定义区域(DeveloperArea):包含开发者定义字段列表和开发者字典(用于存储开发者定义字段的值),该区域为

    2022年4月9日
    44
  • velocity调用java静态方法_java模板引擎

    velocity调用java静态方法_java模板引擎跟学习其它技术一样,首先到官网去下载必要的包,下载地址:http://velocity.apache.org/download.cgi目前使用的是velocity1.6.3,由于自己的E文水平一般,在使用之前也到网上搜索了相关文章,然后根据前辈们的指导和自己的实践结合.记录下此文,以便以后能快速回忆.一、在eclipse中新建一个工程,把包velocity-1.6.3.jar到在WEB-INF…

    2022年10月20日
    3

发表回复

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

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