Kaydet (Commit) 8856ddae authored tarafından Gregory P. Smith's avatar Gregory P. Smith

Adds a Thread.getIdent() method to provide the _get_ident() value for

any given threading.Thread object.  feature request issue 2871.
üst 1bd52d74
......@@ -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.
......
......@@ -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)
......
......@@ -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
......
......@@ -67,6 +67,8 @@ Extension Modules
- Issue #2870: cmathmodule.c compile error.
- Added a threading.Thread.getIdent() method.
Library
-------
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment