WeakValueDict and threadsafety
Duncan Booth
duncan.booth at invalid.invalid
Sat Dec 10 11:19:36 EST 2011
Darren Dale <dsdale24 at gmail.com> wrote:
> 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?
You can't, but it shouldn't matter.
So long as you have a strong reference in 'data' that particular object
will continue to exist. Other entries in 'registry' might disappear while
you are holding your lock but that shouldn't matter to you.
What is concerning though is that you are using `id(data)` as the key and
then presumably storing that separately as your `oid` value. If the
lifetime of the value stored as `oid` exceeds the lifetime of the strong
references to `data` then you might get a new data value created with the
same id as some previous value.
In other words I think there's a problem here, but nothing to do with the
lock.
--
Duncan Booth
More information about the Python-list
mailing list