Reputation: 75
This may be a 'Python Web Programming 101' question, but I'm confused about some code in the aeoid project (http://github.com/Arachnid/aeoid). here's the code:
_current_user = None
def get_current_user():
"""Returns the currently logged in user, or None if no user is logged in."""
global _current_user
if not _current_user and 'aeoid.user' in os.environ:
_current_user = User(None, _from_model_key=os.environ['aeoid.user'])
return _current_user
But my understanding was that global variables were, ehm, global! And so different requests from different users could (potentially) access and update the same value, hence the need for sessions, in order to store per-user, non-global variables. So, in the code above, what prevents one request from believing the current user is the user set by another request? Sorry if this is basic, it's just not how i thought things worked.
Thanks
Upvotes: 1
Views: 1041
Reputation: 101149
The App Engine Python runtime is single-threaded - only a single request is processed, per runtime instance, at a time. As a result, you can use globals for request-specific parameters, so long as you take care to reset them at the beginning of each request, so they don't leak data from one request to another.
Upvotes: 2
Reputation: 3284
You are not the only one confused about globals on appengine. But I know the os.environ is unique to each request, so I think that could explain this code working right. If not, it might be so that the module this is in gets forced reloaded somehow, a trick I'm looking in too for dynamic settings in my project.
Upvotes: 0