Unique thread ID

Discussion in 'Python' started by Benjamin, Jan 18, 2008.

  1. Benjamin

    Benjamin Guest

    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.
     
    Benjamin, Jan 18, 2008
    #1
    1. Advertising

  2. 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
     
    Diez B. Roggisch, Jan 18, 2008
    #2
    1. Advertising

  3. 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
     
    Christian Heimes, Jan 18, 2008
    #3
  4. Benjamin

    Benjamin Guest

    On Jan 18, 2:31 am, Christian Heimes <> 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
     
    Benjamin, Jan 19, 2008
    #4
  5. En Fri, 18 Jan 2008 22:41:47 -0300, Benjamin <>
    escribió:

    > On Jan 18, 2:31 am, Christian Heimes <> 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
     
    Gabriel Genellina, Jan 19, 2008
    #5
  6. Benjamin

    Benjamin Guest

    On Jan 18, 8:31 pm, "Gabriel Genellina" <>
    wrote:
    > En Fri, 18 Jan 2008 22:41:47 -0300, Benjamin <>
    > escribió:
    >
    > > On Jan 18, 2:31 am, Christian Heimes <> 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
     
    Benjamin, Jan 19, 2008
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Charles A. Lackman

    Terminating a thread from the main thread

    Charles A. Lackman, Dec 9, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    1,548
    Herfried K. Wagner [MVP]
    Dec 9, 2004
  2. pawo
    Replies:
    0
    Views:
    502
  3. Stephen Miller
    Replies:
    3
    Views:
    3,977
    Stephen Miller
    Jul 2, 2004
  4. ToshiBoy
    Replies:
    6
    Views:
    851
    ToshiBoy
    Aug 12, 2008
  5. Token Type
    Replies:
    9
    Views:
    360
    Chris Angelico
    Sep 9, 2012
Loading...

Share This Page