threading issues with statcache

Randall Kern randy at spoke.net
Sat Jan 27 19:20:26 EST 2001


Looking at the code for the statcache module from py 1.5.2, it looks like it
isn't thread safe.  While writing my own substitute, I realized I am unclear
on when names are looked up.

In particular, given two function like these (copied from statcache):

cache = {}
def stat(path):
    if cache.has_key(path):
        return cache[path]

    cache[path] = ret = os.stat(path)
    return ret

def reset():
    global cache
    cache = {}


If the symbol 'cache' is looked up _once_ per function, then these two
functions may be used across multiple threads.  If it is looked up for every
reference, than it would be possible to call reset() between a TRUE
has_key() and the return in stat(), which would result in a KeyError.

When are global variable's bound?

Thanks,
-Randy
P.S. I realize that stat() might update the cache more than once for the
same path, if it was called from multiple threads. But that's consistent
with it's expected behavior, so not really a MT problem.





More information about the Python-list mailing list