Understanding global object persistence in Python WSGI apps

前端 未结 3 818
庸人自扰
庸人自扰 2021-02-05 20:01

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

count = 0

class MyHandler(webapp2.RequestHandler):

    def get(self):

        glob         


        
3条回答
  •  孤城傲影
    2021-02-05 20:49

    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.

提交回复
热议问题