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) ...
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.