gpt4 book ai didi

python - PEP 333 应用程序实例和对象

转载 作者:行者123 更新时间:2023-11-28 17:14:33 26 4
gpt4 key购买 nike

我最近一直在尝试学习 WSGI 以及网络如何在 Python 方面工作。所以我一直在阅读 Werkzeug 和 PEP333 来学习。

但是我遇到了一个小问题,我认为我理解但可能不理解,所以我希望您能指导正确的方向。

PEP333 指出:

The application object is simply a callable object that accepts two arguments. The term "object" should not be misconstrued as requiring an actual object instance: a function, method, class, or instance with a call method are all acceptable for use as an application object. Application objects must be able to be invoked more than once, as virtually all servers/gateways (other than CGI) will make such repeated requests.

实现:

class AppClass:
"""Produce the same output, but using a class

(Note: 'AppClass' is the "application" here, so calling it
returns an instance of 'AppClass', which is then the iterable
return value of the "application callable" as required by
the spec.

If we wanted to use *instances* of 'AppClass' as application
objects instead, we would have to implement a '__call__'
method, which would be invoked to execute the application,
and we would need to create an instance for use by the
server or gateway.
"""

def __init__(self, environ, start_response):
self.environ = environ
self.start = start_response

def __iter__(self):
status = '200 OK'
response_headers = [('Content-type', 'text/plain')]
self.start(status, response_headers)
yield "Hello world!\n"

我在这里的问题只是为了澄清我是否理解正确。

它声明AppClass是应用程序,当我们调用它时,它返回一个AppClass的实例。但是再往下说“如果我们想使用 AppClass 的实例代替应用程序对象”,这是不是说当 WSGI 的服务器端调用 AppClass 对象时,只有一个实例在运行?

例如。服务器可以向应用程序发出多个请求(200 OK)以获得更多响应,这就是将 iter 放入类中的原因。但是每个请求都通过相同的单个 AppClass 实例运行,每个对服务器的请求基本上不会实例化多个 AppClass 实例?

对不起,如果这是冗长的话,如果我没有说清楚的话,再次道歉。我正在努力改进 atm。

一如既往地感谢您的意见。

谢谢。

最佳答案

服务器技术将为每个 请求调用您的app(在本例中为类AppClass,导致对象构造)。这是因为每个请求都可能具有唯一的 environ

关于此的巧妙之处在于它并不意味着您的 app 必须是一个类,我经常发现将我的 wsgi 应用程序(或中间件)定义为返回函数的函数很有用:

# I'd strongly suggest using a web framework instead to define your application
def my_application(environ, start_response):
start_response(str('200 OK'), [(str('Content-Type'), str('text/plain'))])
return [b'hello world!\n']

def my_middleware(app):
def middleware_func(environ, start_response):
# do something or call the inner app
return app(environ, start_response)
return middleware_func

# expose `app` for whatever server tech you're using (such as uwsgi)
app = my_application
app = my_middleware(app)

另一种常见的模式涉及定义一个对象来存储一些应用程序状态,该状态只构建一次:

class MyApplication(object):
def __init__(self):
# potentially some expensive initialization
self.routes = ...

def __call__(self, environ, start_response):
# Called once per request, must call `start_response` and then
# return something iterable -- could even be `return self` if
# this class were to define `__iter__`
...
return [...]

app = MyApplication(...)

至于 PEP333,我建议阅读 PEP3333相反——它包含大部分相同的信息,但阐明了整个过程中使用的数据类型。

关于python - PEP 333 应用程序实例和对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45133698/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com