[Python-Dev] Revised Proposal: thread.get_dict

Jim Fulton jim at zope.com
Wed Jun 30 10:24:26 EDT 2004


(This revised proposal is based on a suggestion from Armin
  Rigo.)

We often want to associate data with threads.  Python
manages a dictionary per-tread for this purpose, but there's
currently no way to get to this dictionary from Python.

I propose, for 2.4, to add a "local" class to the thread
module for creating thread-local objects,

Here is proposed documentation (and test:) for the class:

"""Thread-local objects

Thread-local objects support the management of thread-local data.
If you have data that you want to be local to a thread, simply create
a thread-local object and use it's attributes:

   >>> thread
   >>> mydata = thread.local()
   >>> mydata.number = 42
   >>> mydata.number
   42

You can also access the local-object's dictionary:

   >>> mydata.__dict__
   {'number': 42}
   >>> mydata.__dict__.setdefault('widgets', [])
   []
   >>> mydata.widgets
   []

What's important about thread-local objects is that their data are
local to a thread. If we access the data in a different thread:

   >>> log = []
   >>> def f():
   ...     log.append(mydata.__dict__.items())
   ...     mydata.number = 11
   ...     log.append(mydata.number)

   >>> import threading
   >>> thread = threading.Thread(target=f)
   >>> thread.start()
   >>> thread.join()
   >>> log
   [[], 11]

we get different data.  Furthermore, changes made in the other thread
don't affect data seen in this thread:

   >>> mydata.number
   42

Of course, values you get from a local object, including a __dict__
attribute, are for whatever thread was current at the time the
attribute was read.  For that reason, you generally don't want to save
these values across threads, as they apply only to the thread they
came from.

"""

Here is a demonstration Python implementation:

   from threading import currentThread

   class local(object):

     def __init__(self):
         object.__setattr__(self, '_local__key', str(id(self)))

     def __getattribute__(self, name):
         key = object.__getattribute__(self, '__dict__')['_local__key']
         d = currentThread().__dict__.get(key)
         if d is None:
             d = {}
             currentThread().__dict__[key] = d
         if name == '__dict__':
             return d
         try:
             return d[name]
         except KeyError:
             raise AttributeError, name

     def __setattr__(self, name, value):
         key = object.__getattribute__(self, '__dict__')['_local__key']
         d = currentThread().__dict__.get(key)
         if d is None:
             d = {}
             currentThread().__dict__[key] = d
         if name == '__dict__':
             raise AttributeError, "Can't assign to __dict__"
         d[name] = value

The actual implementation will be written in C using PyThreadState_GetDict.

Thoughts?  Do I need to create a PEP?

Jim

-- 
Jim Fulton           mailto:jim at zope.com       Python Powered!
CTO                  (540) 361-1714            http://www.python.org
Zope Corporation     http://www.zope.com       http://www.zope.org



More information about the Python-Dev mailing list