[Python-Dev] Version 3 Proposal: thread-local data

Tim Peters tim.one at comcast.net
Thu Jul 1 00:12:27 EDT 2004


[Jim]
...
>         def __del__(self):
>             key = object.__getattribute__(self, '_local__key')
>             for thread in enumerate():
>                 if key in thread.__dict__:
>                     del thread.__dict__[key]

Note that a __del__ method should never reference a module global
"enumerate" in this case) -- it's all but certain to lead to "iteration over
non-sequence" Mystery Errors at Python shutdown time (due to module globals
getting "None'd out").  The conventional workaround is to give the class an
attribute initialized from the module global; e.g.,

class local(_localbase):
    _enumerate = threading.enumerate

    ...

    def __del__(self):
        ...
        for thread in self._enumerate():

The conventional workaround that doesn't work is to spell that

        for thread in local._enumerate():

instead; it doesn't work because "local" is also a module global.





More information about the Python-Dev mailing list