If I have a RequestHandler
class, that has a variable (self.var
below) that is initialized during a request. Can that variable be overwritten in a concurrent environment (threadsafe=true
)? E.g.
class MyRequestHandler(webapp2.RequestHandler):
var = None
def get(self, id):
self.var = value_from_datastore(id)
# Do something that takes time
# ...
self.response.write.out(self.var)
self.response.write.out(self.var2)
Can self.var
be overwritten by a second request (presumably from a different user) between when it is set at the top of the get
method and when it's used in the output?
Thanks,
Baird
Update:
I was under the (mistaken) impression that the var = None
was declaring it as an instance variable. Thanks for correcting my (gaping) misconception ;-)
If I do the "declaration" in the __init__
, am I better off? I think it comes down to if MyRequestHandler is created for each thread, or if there is only one for all the shared threads. Can there still be interference from different requests by different users?
class MyRequestHandler(webapp2.RequestHandler):
def __init__(self):
self.var = None
def get(self, id):
self.var = value_from_datastore(id)
# Do something that takes time
# ...
self.response.write.out(self.var)
self.response.write.out(self.var2)
Thanks,
Baird