[Python-Dev] Proposal: thread.get_dict

Jim Fulton jim at zope.com
Mon Jun 28 13:58:56 EDT 2004


Phillip J. Eby wrote:
> At 10:50 AM 6/28/04 -0400, Jim Fulton wrote:
> 
>> 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 get_dict method to the thread
>> module that will return this dictionary.  This will be
>> implemented via a call to PyThreadState_GetDict.
>>
>> Any objections?
> 
> 
> What's wrong with using (e.g. setting attributes on) 
> 'threading.currentThread()'?  It's already implemented and available in 
> 2.3 (and numerous prior versions).

But it's relatively expensive.  You have to call threading.currentThread,
Which then has to do a dict lookup.  Plus, when you get the thread, you have to
pick attribute names that won't conflict, which means that you either have ugly
attribute names or introduce an intermediate object to provide a separate
namespace, which ads more cost.

OTOH, Python has this nice per-thread dict that wants to be used for thread
data but is unexposed.

As far as 2.3 support, we'll provide something like this in Zope, but
I'd like to get it into Python so that we can stop providing it in Zope
some time in the future.

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