summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Doc/library/threading.rst11
-rw-r--r--Lib/test/test_threading.py5
-rw-r--r--Lib/threading.py12
-rw-r--r--Misc/NEWS2
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("<thread %d>"%i, self, sema, mutex, numrunning)
threads.append(t)
+ self.failUnlessEqual(t.getIdent(), None)
+ self.assert_(re.match('<TestThread\(.*, initial\)>', 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('<TestThread\(.*, \w+ -?\d+\)>', 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
-------