[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Unique thread ID

Benjamin

1/18/2008 3:46:00 AM

Is there a way to obtain a unique ID for the current thread? I have an
object that I need to store local thread data in, and I don't want to
use threading.local because each thread might have multiple instances
of my object.
5 Answers

Diez B. Roggisch

1/18/2008 8:15:00 AM

0

Benjamin schrieb:
> Is there a way to obtain a unique ID for the current thread? I have an
> object that I need to store local thread data in, and I don't want to
> use threading.local because each thread might have multiple instances
> of my object.

You can use the thread itself, it's usable as key (that's what
threadlocals use for their implementation anyway)

But I don't understand your problem - all thread-local implementations I
know of (is there a standard?) can be instantiated in each object and
then offer a get/set method (keyed or not) - but without interfering
with outher instances of themselves of course.


Diez

Christian Heimes

1/18/2008 8:31:00 AM

0

Benjamin wrote:
> Is there a way to obtain a unique ID for the current thread? I have an
> object that I need to store local thread data in, and I don't want to
> use threading.local because each thread might have multiple instances
> of my object.

threading.get_ident() but please use threading.local. Nobody is going to
stop you if you use a list or dict in threading.local.

Christian

Benjamin

1/19/2008 1:42:00 AM

0

On Jan 18, 2:31 am, Christian Heimes <li...@cheimes.de> wrote:
> Benjamin wrote:
> > Is there a way to obtain a unique ID for the current thread? I have an
> > object that I need to store local thread data in, and I don't want to
> > use threading.local because each thread might have multiple instances
> > of my object.
>
> threading.get_ident() but please use threading.local. Nobody is going to
> stop you if you use a list or dict in threading.local.
then, I have to figure out how to represent an instance of my object
in threading.local. (The data also won't be garbage collect when my
object is, will it?) I think the unique id is elegant in this case.
>
> Christian

Gabriel Genellina

1/19/2008 2:32:00 AM

0

En Fri, 18 Jan 2008 22:41:47 -0300, Benjamin <musiccomposition@gmail.com>
escribió:

> On Jan 18, 2:31 am, Christian Heimes <li...@cheimes.de> wrote:
>> Benjamin wrote:
>> > Is there a way to obtain a unique ID for the current thread? I have an
>> > object that I need to store local thread data in, and I don't want to
>> > use threading.local because each thread might have multiple instances
>> > of my object.
>>
>> threading.get_ident() but please use threading.local. Nobody is going to
>> stop you if you use a list or dict in threading.local.
> then, I have to figure out how to represent an instance of my object
> in threading.local. (The data also won't be garbage collect when my
> object is, will it?) I think the unique id is elegant in this case.

I think you didn't get how threading.local works yet. It's a lot easier
than you imply. Just store your instances as attributes of a
threading.local object. You may use a list or dictionary if you want
multiple instances.
Read _threading_local.py, it contains a lot of examples.

--
Gabriel Genellina

Benjamin

1/19/2008 3:47:00 AM

0

On Jan 18, 8:31 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
> En Fri, 18 Jan 2008 22:41:47 -0300, Benjamin <musiccomposit...@gmail.com>
> escribió:
>
> > On Jan 18, 2:31 am, Christian Heimes <li...@cheimes.de> wrote:
> >> Benjamin wrote:
> >> > Is there a way to obtain a unique ID for the current thread? I have an
> >> > object that I need to store local thread data in, and I don't want to
> >> > use threading.local because each thread might have multiple instances
> >> > of my object.
>
> >> threading.get_ident() but please use threading.local. Nobody is going to
> >> stop you if you use a list or dict in threading.local.
> > then, I have to figure out how to represent an instance of my object
> > in threading.local. (The data also won't be garbage collect when my
> > object is, will it?) I think the unique id is elegant in this case.
>
> I think you didn't get how threading.local works yet. It's a lot easier
> than you imply. Just store your instances as attributes of a
> threading.local object. You may use a list or dictionary if you want
> multiple instances.
> Read _threading_local.py, it contains a lot of examples.
You're correct. I misread the documentation. I now understand how it
works and am using it. Thanks for pointing me down the right path.
>
> --
> Gabriel Genellina