Unique thread ID

B

Benjamin

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.
 
D

Diez B. Roggisch

Benjamin said:
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
 
C

Christian Heimes

Benjamin said:
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
 
B

Benjamin

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.
 
G

Gabriel Genellina

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.
 
B

Benjamin

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top