WeakValueDict and threadsafety

Darren Dale dsdale24 at gmail.com
Sat Dec 10 07:41:13 EST 2011


I am using a WeakValueDict in a way that is nearly identical to the
example at the end of http://docs.python.org/library/weakref.html?highlight=weakref#example
, where "an application can use objects IDs to retrieve objects that
it has seen before. The IDs of the objects can then be used in other
data structures without forcing the objects to remain alive, but the
objects can still be retrieved by ID if they do." My program is
multithreaded, so I added the necessary check for liveliness that was
discussed at http://docs.python.org/library/weakref.html?highlight=weakref#weak-reference-objects
. Basically, I have:

import threading
import weakref

registry = weakref.WeakValueDictionary()
reglock = threading.Lock()

def get_data(oid):
    with reglock:
        data = registry.get(oid, None)
        if data is None:
            data = make_data()
            registry[id(data)] = data
    return data

I'm concerned that this is not actually thread-safe. When I no longer
hold strong references to an instance of data, at some point the
garbage collector will kick in and remove that entry from my registry.
How can I ensure the garbage collection process does not modify the
registry while I'm holding the lock?

Thanks,
Darren



More information about the Python-list mailing list