了解Python WSGI应用程序中的全局对象持久性 [英] Understanding global object persistence in Python WSGI apps

查看:179
本文介绍了了解Python WSGI应用程序中的全局对象持久性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请考虑Google App Engine中WebApp2应用程序中的以下代码:

  count = 0 

class MyHandler(webapp2.RequestHandler):

def get(self):

全局计数$ b $ count = count + 1
print count

随着页面的每次刷新,计数都会增加。



我来自PHP世界,每个请求都是新的全球环境。我理解这里发生的是,因为我正在使用WebApp2的wsgi配置,所以Python不会在每个请求中启动一个新进程。另一方面,如果我使用cgi配置,全局环境会每次重新实例化,例如PHP ...

假设上述内容是正确的(If不是,请纠正我)... $ / b>


  1. 我该如何处理需要一个全局变量的场景的要求?我可以在RequestHandler类中放入一个实例变量,但是像导入的实用程序模块这样的东西,它使用全局变量来存储消息对象之类的东西?
  2. 是否有某种技术重置所有变量,或强制重新实例化环境?

  3. 全局环境是无限期存在的,还是在某个时刻重置自身?

  4. 这个GAE是特定的,还是wsgi全局持久性在任何服务器场景中的工作都是相同的?

编辑:



下面是使用threadlocal的一个尝试:

  count = 0 

mydata = threading.local()
mydata.count = 0

class MyHandler(webapp2.RequestHandler):

def get(self):

全局计数
计数= count + 1
打印计数

mydata.count = mydata.count + 1
print mydata.count

这些也会在请求中增加。

解决方案

您的理解是正确的。如果你希望变量在请求期间持续存在,你就不应该让它们成为全局变量 - 在你的RequestHandler类上创建它们的实例变量,访问为 self.var 。由于每个请求都实例化一个新的RequestHandler,因此只要您需要它们,您的变量就会一直存在。除非您确实需要全局(而不是特定于请求的)范围,否则最好避免使用全局变量。



另请注意,您的App Engine应用程序将运行在多个服务器上;全局变量只能在同一服务器内的请求中访问。


Consider the following code in my WebApp2 application in Google App Engine:

count = 0

class MyHandler(webapp2.RequestHandler):

    def get(self):

        global count
        count = count + 1
        print count

With each refresh of the page, the count increments higher.

I'm coming from the PHP world where every request was a new global environment. What I understand to be happening here is, because I'm using the wsgi configuration for WebApp2, Python does not kick off a new process on each request. If I was using a cgi configuration, on the other hand, the global environment would re-instantiate each time, like PHP...

Assuming the above is correct (If not, please correct me) ...

  1. How could I handle scenarios where I'd want a global variable that persisted only for the lifetime of the request? I could put an instance variable in the RequestHandler class, but what about things like utility modules that I import that use global vars for things like storing a message object?
  2. Is there some sort of technique to reset all variables, or to force a re-instantiation of the environment?
  3. Does the global environment persist indefinitely, or does it reset itself at some point?
  4. Is any of this GAE specific, or does wsgi global persistance work the same in any server scenario?

EDIT:

Here's an attempt using threadlocal:

count = 0

mydata = threading.local()
mydata.count = 0

class MyHandler(webapp2.RequestHandler):

    def get(self):

        global count
        count = count + 1
        print count

        mydata.count = mydata.count + 1
        print mydata.count

These also increment across requests

解决方案

Your understanding is correct. If you want variables that persist for the duration of the request, you shouldn't make them globals at all - make them instance variables on your RequestHandler class, accessed as self.var. Since a new RequestHandler is instantiated for each request, your variables will stick around exactly as long as you need them to. Global variables are best avoided unless you really do need global (as opposed to request-specific) scope.

Also note that your App Engine app will run on multiple servers; globals are only accessible to requests inside the same server.

这篇关于了解Python WSGI应用程序中的全局对象持久性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆