为什么 CherryPy 对象属性在请求之间保持不变?

发布于 2024-09-27 03:36:40 字数 886 浏览 2 评论 0原文

我正在为我的 CherryPy 应用程序编写调试方法。有问题的代码(非常)基本上与此相同:

import cherrypy

class Page:
    def index(self):
        try:
            self.body += 'okay'
        except AttributeError:
            self.body = 'okay'
        return self.body
    index.exposed = True

cherrypy.quickstart(Page(), config='root.conf')

我惊讶地发现,从一个请求到另一个请求, self.body 的输出不断增长。当我从一个客户端访问该页面,然后从另一个同时打开的客户端访问该页面,然后刷新这两个客户端的浏览器时,输出是不断增加的“okay”字符串。在我的调试方法中,我还记录了特定于用户的信息(即会话数据),并且这些信息也显示在两个用户的输出中。

我假设这是因为 python 模块被加载到工作内存中,而不是针对每个请求重新运行。

我的问题是:这是如何运作的?为什么 self.debug 在请求之间被保留,而cherrypy.session 和cherrypy.response 却没有?

有没有办法设置一个仅用于当前请求的对象属性?我知道我可以根据每个请求覆盖 ​​self.body,但这似乎有点临时。 CherryPy 中有标准或内置的方法吗?

(第二个问题移至如何CherryPy 缓存有用吗?

I was writing debugging methods for my CherryPy application. The code in question was (very) basically equivalent to this:

import cherrypy

class Page:
    def index(self):
        try:
            self.body += 'okay'
        except AttributeError:
            self.body = 'okay'
        return self.body
    index.exposed = True

cherrypy.quickstart(Page(), config='root.conf')

I was surprised to notice that from request to request, the output of self.body grew. When I visited the page from one client, and then from another concurrently-open client, and then refreshed the browsers for both, the output was an ever-increasing string of "okay"s. In my debugging method, I was also recording user-specific information (i.e. session data) and that, too, showed up in both users' output.

I'm assuming that's because the python module is loaded into working memory instead of being re-run for every request.

My question is this: How does that work? How is it that self.debug is preserved from request to request, but cherrypy.session and cherrypy.response aren't?

And is there any way to set an object attribute that will only be used for the current request? I know I can overwrite self.body per every request, but it seems a little ad-hoc. Is there a standard or built-in way of doing it in CherryPy?

(second question moved to How does CherryPy caching work?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你的呼吸 2024-10-04 03:36:40

Synthesizerpatel 的分析是正确的,但如果您确实想为每个请求存储一些数据,请将其存储为 Cherrypy.request 上的属性,而不是会话中。 cherrypy.request.response 对象对于每个请求来说都是新的,因此不必担心它们的任何属性会在请求之间持续存在。这是执行此操作的规范方法。只要确保您没有覆盖 Cherrypy 的任何内部属性即可!例如,cherrypy.request.body 已被保留用于向您传递 POSTed JSON 请求正文。

有关范围界定的具体工作原理的所有详细信息,最好的来源是 来源代码

synthesizerpatel's analysis is correct, but if you really want to store some data per request, then store it as an attribute on cherrypy.request, not in the session. The cherrypy.request and .response objects are new for each request, so there's no fear that any of their attributes will persist across requests. That is the canonical way to do it. Just make sure you're not overwriting any of cherrypy's internal attributes! cherrypy.request.body, for example, is already reserved for handing you, say, a POSTed JSON request body.

For all the details of exactly how the scoping works, the best source is the source code.

您从 self.body 中获得了相同的数据,这一发现一语中的,因为它在运行 CherryPy 的 Python 进程的内存中是相同的。

self.debug 因此维护“状态”,它是正在运行的服务器的一个属性。

要设置当前会话的数据,请使用cherrypy.session['fieldname'] = 'fieldvalue',要获取数据,请使用cherrypy.session.get('fieldname')。

您(程序员)不需要知道会话 ID,cherrypy.session 会为您处理——会话 ID 是由cherrypy 自动动态生成的,并通过在后续查询时在浏览器和服务器之间交换 cookie 来保存/响应交互。

如果您没有在配置中为cherrypy.session指定storage_type,它将存储在内存中(服务器和您都可以访问),但如果您愿意,您也可以将会话文件存储在磁盘上,这可能会很方便一种调试方式,无需编写一堆代码即可从正在运行的服务器中挖掘会话 ID 或密钥/对值。

有关更多信息,请查看 http://www.cherrypy.org/wiki/CherryPySessions

You hit the nail on the head with the observation that you're getting the same data from self.body because it's the same in memory of the Python process running CherryPy.

self.debug maintains 'state' for this reason, it's an attribute of the running server.

To set data for the current session, use cherrypy.session['fieldname'] = 'fieldvalue', to get data use cherrypy.session.get('fieldname').

You (the programmer) do not need to know the session ID, cherrypy.session handles that for you -- the session ID is automatically generated on the fly by cherrypy and is persisted by exchanging a cookie between the browser and server on subsequent query/response interactions.

If you don't specify a storage_type for cherrypy.session in your config, it'll be stored in memory (accessible to the server and you), but you can also store the session files on disk if you wish which might be a handy way for you to debug without having to write a bunch of code to dig out session IDs or key/pair values from the running server.

For more info check out http://www.cherrypy.org/wiki/CherryPySessions

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文