You don't have to use the "global" keyword because this is Python, not PHP.
If you wanted to use that variable in another module, you would have to
import it like this:

from module_name import townCache

It's possible for two operations to update townCache concurrently, but in
your case it looks like it doesn't really matter. If TownModel is somehow
updated between reads, it's theoretically possible for you to have an older
TownModel in the local cache, but if you're going to store something in the
cache with no expiration, it sounds like you don't care about this case
anyway.

Two code tips:

- a general python convention is to use underscore_case, not CamelCase.
CamelCase is reserved for class names. I'd rename townCache to town_cache
and getTown to get_town

- you can probably get away with calling TownModel Town

Ikai Lan
Developer Programs Engineer, Google App Engine
Blog: http://googleappengine.blogspot.com
Twitter: http://twitter.com/app_engine
Reddit: http://www.reddit.com/r/appengine



On Wed, Jun 29, 2011 at 5:40 PM, Joshua Smith <[email protected]>wrote:

> I have this code in one of my apps:
>
> townCache = {}
> def getTown(id):
>  if not id in townCache:
>   townCache[id] = TownModel.get_by_id(id)
>  return townCache[id]
>
> Is this thread safe?  I think it is, because the worst that happens is the
> assignment happens redundantly with the same data.
>
> Random other question: Why don't I have to say "global townCache" at the
> top of that function?
>
> On Jun 29, 2011, at 7:43 AM, Joshua Smith wrote:
>
> > I would assume they are the same as the basic principles of thread safety
> in any language:
> >
> > - Don't rely on global state, because multiple of your functions might be
> running simultaneously
> >
> > This usually isn't very hard to achieve - just pass parameters instead of
> modifying globals.  The places where it can get tricky are where you really
> *want* to use global state, such as for an in-memory cache.  Usually the
> language provides some primitives to ensure that only one thread at a time
> is updating the cache.  It appears that python gives you thread-safety for a
> lot of cases:
> >
> > http://effbot.org/zone/thread-synchronization.htm
> >
> > On Jun 29, 2011, at 1:20 AM, Greg wrote:
> >
> >> Hi -
> >>
> >> Could anyone familiar with threads explain the basic principals of
> >> python thread-safety?
> >>
> >> Cheers!
> >> Greg.
> >>
> >> --
> >> You received this message because you are subscribed to the Google
> Groups "Google App Engine" group.
> >> To post to this group, send email to [email protected].
> >> To unsubscribe from this group, send email to
> [email protected].
> >> For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.
> >>
> >
> > --
> > You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> > To post to this group, send email to [email protected].
> > To unsubscribe from this group, send email to
> [email protected].
> > For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.
> >
>
> --
> You received this message because you are subscribed to the Google Groups
> "Google App Engine" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/google-appengine?hl=en.
>
>

-- 
You received this message because you are subscribed to the Google Groups 
"Google App Engine" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-appengine?hl=en.

Reply via email to