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


相关推荐

  • HashMap扩容流程[通俗易懂]

    HashMap扩容流程[通俗易懂]文章目录为什么扩容?什么时候扩容?如何扩容?今天在和同时讨论HashMap的时候,提到了扩容和冲哈希的事情,然后我发现大家都是一种半懂不懂的状态。于是回去做了一番功课,写下这篇文章。HashMap的扩容,又被很多人叫rehash、重哈希,我本人是很反对这个叫法的,事实上HashMap扩容的时候,Node中存储的Key的hash值并没有发生变化,只是Node的位置发生了变化。首先说为什么需要扩…

    2025年11月23日
    4
  • 计算机网络之ip、子网掩码、网络号、主机号等概念解析

    计算机网络之ip、子网掩码、网络号、主机号等概念解析在工作中谈论到计算机网络时,有几个经常出现的术语,比如:ip、子网掩码、网段等等。之前对这些概念的理解都比较模糊,只知其大概意思,随着工作中遇到的网络问题越来越多,有必要详细理解一下计算机网络的基础知识了。这篇文章就先介绍几个计算机网络领域的专业术语。IP地址ip这个词是计算机网络中出现频率最高的了,甚至只要使用过电脑的人都知道这个词。IP地址全程是互联网协议地址(英文:InternetPr…

    2022年6月24日
    39
  • 微创业平台

    一种专为大学生创业服务的平台。主旨是,让大学生创业创新,在某种比较简单,比较容易的环境里进行。让大学生创业创新,不要太多的风险,比较容易达成创业目标。云电话,一种5G新电话。特别适合大学生微创业,更需要大学生微创新。将云电话平台,建设成湖铁微创业平台,是深圳云电话平台,与湖铁职院创新创业学院,合作打造的专为湖铁职院大学生,创新创业服务的平台。建设好湖铁微创业平台,具有重大的社会意义。√成功示范作用。先在创新创业学院,打造标杆试点MVP。√便…

    2022年4月4日
    89
  • servu搭建ftp服务器教程_ftp端口映射

    servu搭建ftp服务器教程_ftp端口映射映口映射的功能主要就是实际互联网可以访问当前进行映射的电脑,首先我们要进行路由器的端口映射,常见的开放端口有80,21,3389等,80是网站常用端口,21是FTP服务器常用端口,3389是远程桌面连接端口。下面为大家详细讲解端口映射的具体方法!1,首先我们要了解路由器的配置介面的IP地址,常用的是192.168.0.1或192.168.1.1,假如你不知道自己的路由器的配置页面的IP

    2025年10月29日
    0
  • scipy.interpolate.interp1d()函数详解

    scipy.interpolate.interp1d()函数详解插值模块scipy.interpolate是插值模块,插值是离散函数逼近的重要方法,利用它可通过函数在有限个点处的取值状况,估算出函数在其他点处的近似值。与拟合不同的是,要求曲线通过所有的已知数据。计算插值有两种基本的方法:对一个完整的数据集去拟合一个函数;仿样内插法:对数据集的不同部分拟合出不同的函数,而函数之间的曲线平滑对接。SciPy的interpolate模块提供了许多对数…

    2022年6月9日
    199
  • 由于Redis后门漏洞导致服务器被注入挖矿脚本解决过程

    由于Redis后门漏洞导致服务器被注入挖矿脚本解决过程由于Redis后门漏洞导致服务器被注入挖矿脚本解决过程事件描述某一天的早晨,我还是像往常一样搭着公交车开启打工仔的一天,一早8.30就到办公室了,坐着玩手机等上班,就这这时突然我组长飞快的回来办公室,回来就说快看看阿里云后台服务,服务是不是挂掉了,我当时就纳闷了一大早的流量不大怎么就宕机了呢,不一会我组长收到了阿里云短信通知监测到恶意脚本,接下来就是脚本的查找前期处理首先是通过阿里云的控制台发现,查看到恶意的进程PID,通过ps-ef|greap5724的确看到了当前进程,前期处理我只

    2022年7月14日
    20

发表回复

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

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