mirror of
https://github.com/python/cpython.git
synced 2025-11-26 13:22:51 +00:00
Adds a Thread.getIdent() method to provide the _get_ident() value for
any given threading.Thread object. feature request issue 2871.
This commit is contained in:
parent
1bd52d745b
commit
8856ddae25
4 changed files with 28 additions and 2 deletions
|
|
@ -651,6 +651,17 @@ impossible to detect the termination of alien threads.
|
||||||
constructor.
|
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()
|
.. method:: Thread.isAlive()
|
||||||
|
|
||||||
Return whether the thread is alive.
|
Return whether the thread is alive.
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@
|
||||||
import test.test_support
|
import test.test_support
|
||||||
from test.test_support import verbose
|
from test.test_support import verbose
|
||||||
import random
|
import random
|
||||||
|
import re
|
||||||
import sys
|
import sys
|
||||||
import threading
|
import threading
|
||||||
import thread
|
import thread
|
||||||
|
|
@ -72,6 +73,8 @@ class ThreadTests(unittest.TestCase):
|
||||||
for i in range(NUMTASKS):
|
for i in range(NUMTASKS):
|
||||||
t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning)
|
t = TestThread("<thread %d>"%i, self, sema, mutex, numrunning)
|
||||||
threads.append(t)
|
threads.append(t)
|
||||||
|
self.failUnlessEqual(t.getIdent(), None)
|
||||||
|
self.assert_(re.match('<TestThread\(.*, initial\)>', repr(t)))
|
||||||
t.start()
|
t.start()
|
||||||
|
|
||||||
if verbose:
|
if verbose:
|
||||||
|
|
@ -79,6 +82,8 @@ class ThreadTests(unittest.TestCase):
|
||||||
for t in threads:
|
for t in threads:
|
||||||
t.join(NUMTASKS)
|
t.join(NUMTASKS)
|
||||||
self.assert_(not t.isAlive())
|
self.assert_(not t.isAlive())
|
||||||
|
self.failIfEqual(t.getIdent(), 0)
|
||||||
|
self.assert_(re.match('<TestThread\(.*, \w+ -?\d+\)>', repr(t)))
|
||||||
if verbose:
|
if verbose:
|
||||||
print 'all tasks done'
|
print 'all tasks done'
|
||||||
self.assertEqual(numrunning.get(), 0)
|
self.assertEqual(numrunning.get(), 0)
|
||||||
|
|
|
||||||
|
|
@ -414,6 +414,7 @@ class Thread(_Verbose):
|
||||||
self.__args = args
|
self.__args = args
|
||||||
self.__kwargs = kwargs
|
self.__kwargs = kwargs
|
||||||
self.__daemonic = self._set_daemon()
|
self.__daemonic = self._set_daemon()
|
||||||
|
self.__ident = None
|
||||||
self.__started = Event()
|
self.__started = Event()
|
||||||
self.__stopped = False
|
self.__stopped = False
|
||||||
self.__block = Condition(Lock())
|
self.__block = Condition(Lock())
|
||||||
|
|
@ -434,7 +435,9 @@ class Thread(_Verbose):
|
||||||
if self.__stopped:
|
if self.__stopped:
|
||||||
status = "stopped"
|
status = "stopped"
|
||||||
if self.__daemonic:
|
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)
|
return "<%s(%s, %s)>" % (self.__class__.__name__, self.__name, status)
|
||||||
|
|
||||||
def start(self):
|
def start(self):
|
||||||
|
|
@ -481,9 +484,10 @@ class Thread(_Verbose):
|
||||||
|
|
||||||
def __bootstrap_inner(self):
|
def __bootstrap_inner(self):
|
||||||
try:
|
try:
|
||||||
|
self.__ident = _get_ident()
|
||||||
self.__started.set()
|
self.__started.set()
|
||||||
_active_limbo_lock.acquire()
|
_active_limbo_lock.acquire()
|
||||||
_active[_get_ident()] = self
|
_active[self.__ident] = self
|
||||||
del _limbo[self]
|
del _limbo[self]
|
||||||
_active_limbo_lock.release()
|
_active_limbo_lock.release()
|
||||||
if __debug__:
|
if __debug__:
|
||||||
|
|
@ -635,6 +639,10 @@ class Thread(_Verbose):
|
||||||
assert self.__initialized, "Thread.__init__() not called"
|
assert self.__initialized, "Thread.__init__() not called"
|
||||||
self.__name = str(name)
|
self.__name = str(name)
|
||||||
|
|
||||||
|
def getIdent(self):
|
||||||
|
assert self.__initialized, "Thread.__init__() not called"
|
||||||
|
return self.__ident
|
||||||
|
|
||||||
def isAlive(self):
|
def isAlive(self):
|
||||||
assert self.__initialized, "Thread.__init__() not called"
|
assert self.__initialized, "Thread.__init__() not called"
|
||||||
return self.__started.isSet() and not self.__stopped
|
return self.__started.isSet() and not self.__stopped
|
||||||
|
|
|
||||||
|
|
@ -67,6 +67,8 @@ Extension Modules
|
||||||
|
|
||||||
- Issue #2870: cmathmodule.c compile error.
|
- Issue #2870: cmathmodule.c compile error.
|
||||||
|
|
||||||
|
- Added a threading.Thread.getIdent() method.
|
||||||
|
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue