[Python-Dev] Revised Proposal: thread.get_dict

Samuele Pedroni pedronis at bluewin.ch
Wed Jun 30 15:45:31 EDT 2004


At 19:18 30.06.2004 +0100, Armin Rigo wrote:
>Hello Jim,
>
>On Wed, Jun 30, 2004 at 10:24:26AM -0400, Jim Fulton wrote:
> > Here is a demonstration Python implementation:
> >
> >   from threading import currentThread
> >
> >   class local(object):
> >
> >     ...
>
>The instance should clear the entry from all the per-thread dictionaries when
>it is deallocated, to prevent data from outliving it -- and also to prevent
>accidental reuse of the same data by a future instance.
>
>
>Descriptor fun!  I think there is a way to write this class so that "regular"
>attributes like __class__ still work, and without having to special-case
>__dict__.  Something along the lines of:
>
>     def __patch(self):
>         key = "thread-local-%x" % id(self)
>         d = currentThread().__dict__.setdefault(key, {})
>         object.__setattr__(self, '__dict__', d)
>
>     def __getattribute__(self, name):
>         self.__patch()
>         return super(local, self).__getattribute__(name)

I'm puzzled, is this thread-safe?




More information about the Python-Dev mailing list