From 8856ddae2522c644132b5d0730ab60021f2ce13e Mon Sep 17 00:00:00 2001 From: "Gregory P. Smith" Date: Sun, 1 Jun 2008 23:48:47 +0000 Subject: Adds a Thread.getIdent() method to provide the _get_ident() value for any given threading.Thread object. feature request issue 2871. --- Doc/library/threading.rst | 11 +++++++++++ Lib/test/test_threading.py | 5 +++++ Lib/threading.py | 12 ++++++++++-- Misc/NEWS | 2 ++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/Doc/library/threading.rst b/Doc/library/threading.rst index 8cb84b3..a323506 100644 --- a/Doc/library/threading.rst +++ b/Doc/library/threading.rst @@ -651,6 +651,17 @@ impossible to detect the termination of alien threads. constructor. +.. method:: Thread.getIdent() + + Return the 'thread identifier' of this thread or None if the thread has not + been started. This is a nonzero integer. See the :mod:`thread` module's + :func:`get_ident()` function. Thread identifiers may be recycled when a + thread exits and another thread is created. The identifier is returned + even after the thread has exited. + + .. versionadded:: 2.6 + + .. method:: Thread.isAlive() Return whether the thread is alive. diff --git a/Lib/test/test_threading.py b/Lib/test/test_threading.py index 30aa9d5..b974715 100644 --- a/Lib/test/test_threading.py +++ b/Lib/test/test_threading.py @@ -3,6 +3,7 @@ import test.test_support from test.test_support import verbose import random +import re import sys import threading import thread @@ -72,6 +73,8 @@ class ThreadTests(unittest.TestCase): for i in range(NUMTASKS): t = TestThread(""%i, self, sema, mutex, numrunning) threads.append(t) + self.failUnlessEqual(t.getIdent(), None) + self.assert_(re.match('', repr(t))) t.start() if verbose: @@ -79,6 +82,8 @@ class ThreadTests(unittest.TestCase): for t in threads: t.join(NUMTASKS) self.assert_(not t.isAlive()) + self.failIfEqual(t.getIdent(), 0) + self.assert_(re.match('', repr(t))) if verbose: print 'all tasks done' self.assertEqual(numrunning.get(), 0) diff --git a/Lib/threading.py b/Lib/threading.py index d497a46..0d34b6a 100644 --- a/Lib/threading.py +++ b/Lib/threading.py @@ -414,6 +414,7 @@ class Thread(_Verbose): self.__args = args self.__kwargs = kwargs self.__daemonic = self._set_daemon() + self.__ident = None self.__started = Event() self.__stopped = False self.__block = Condition(Lock()) @@ -434,7 +435,9 @@ class Thread(_Verbose): if self.__stopped: status = "stopped" if self.__daemonic: - status = status + " daemon" + status += " daemon" + if self.__ident is not None: + status += " %s" % self.__ident return "<%s(%s, %s)>" % (self.__class__.__name__, self.__name, status) def start(self): @@ -481,9 +484,10 @@ class Thread(_Verbose): def __bootstrap_inner(self): try: + self.__ident = _get_ident() self.__started.set() _active_limbo_lock.acquire() - _active[_get_ident()] = self + _active[self.__ident] = self del _limbo[self] _active_limbo_lock.release() if __debug__: @@ -635,6 +639,10 @@ class Thread(_Verbose): assert self.__initialized, "Thread.__init__() not called" self.__name = str(name) + def getIdent(self): + assert self.__initialized, "Thread.__init__() not called" + return self.__ident + def isAlive(self): assert self.__initialized, "Thread.__init__() not called" return self.__started.isSet() and not self.__stopped diff --git a/Misc/NEWS b/Misc/NEWS index f8e2983..b378956 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -67,6 +67,8 @@ Extension Modules - Issue #2870: cmathmodule.c compile error. +- Added a threading.Thread.getIdent() method. + Library ------- -- cgit v0.12