views:

119

answers:

2

Hello

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

A: 

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.

Koen Bok
`You are not the only one confused about globals on appengine`Always good to know one is not alone! Thanks for the reply.
jerd
+1  A: 

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.

Nick Johnson
Ah, ok. Never had to think about threads before, always seemed a bit of a mystery, but I kind of get it. I'm also looking at werkzeug as a possible replacement for webapp, and I suppose this is what the werkzeug Local object is about, a 'thread-local' object which cleans up at middleware level, and so is 'request-local'. Thanks for the reply. (what I'm tryin to do is to somehow be able to do `self.request.user` and not have the application worry about how that user was authenticated, whether by your own aeoid or some other means.)
jerd