3434_allocate_lock = _thread .allocate_lock
3535_set_sentinel = _thread ._set_sentinel
3636get_ident = _thread .get_ident
37+ try :
38+ get_native_id = _thread .get_native_id
39+ _HAVE_THREAD_NATIVE_ID = True
40+ __all__ .append ('get_native_id' )
41+ except AttributeError :
42+ _HAVE_THREAD_NATIVE_ID = False
3743ThreadError = _thread .error
3844try :
3945 _CRLock = _thread .RLock
@@ -790,6 +796,8 @@ class is implemented.
790796 else :
791797 self ._daemonic = current_thread ().daemon
792798 self ._ident = None
799+ if _HAVE_THREAD_NATIVE_ID :
800+ self ._native_id = None
793801 self ._tstate_lock = None
794802 self ._started = Event ()
795803 self ._is_stopped = False
@@ -891,6 +899,10 @@ def _bootstrap(self):
891899 def _set_ident (self ):
892900 self ._ident = get_ident ()
893901
902+ if _HAVE_THREAD_NATIVE_ID :
903+ def _set_native_id (self ):
904+ self ._native_id = get_native_id ()
905+
894906 def _set_tstate_lock (self ):
895907 """
896908 Set a lock object which will be released by the interpreter when
@@ -903,6 +915,8 @@ def _bootstrap_inner(self):
903915 try :
904916 self ._set_ident ()
905917 self ._set_tstate_lock ()
918+ if _HAVE_THREAD_NATIVE_ID :
919+ self ._set_native_id ()
906920 self ._started .set ()
907921 with _active_limbo_lock :
908922 _active [self ._ident ] = self
@@ -1077,6 +1091,18 @@ def ident(self):
10771091 assert self ._initialized , "Thread.__init__() not called"
10781092 return self ._ident
10791093
1094+ if _HAVE_THREAD_NATIVE_ID :
1095+ @property
1096+ def native_id (self ):
1097+ """Native integral thread ID of this thread, or None if it has not been started.
1098+
1099+ This is a non-negative integer. See the get_native_id() function.
1100+ This represents the Thread ID as reported by the kernel.
1101+
1102+ """
1103+ assert self ._initialized , "Thread.__init__() not called"
1104+ return self ._native_id
1105+
10801106 def is_alive (self ):
10811107 """Return whether the thread is alive.
10821108
@@ -1176,6 +1202,8 @@ def __init__(self):
11761202 self ._set_tstate_lock ()
11771203 self ._started .set ()
11781204 self ._set_ident ()
1205+ if _HAVE_THREAD_NATIVE_ID :
1206+ self ._set_native_id ()
11791207 with _active_limbo_lock :
11801208 _active [self ._ident ] = self
11811209
@@ -1195,6 +1223,8 @@ def __init__(self):
11951223
11961224 self ._started .set ()
11971225 self ._set_ident ()
1226+ if _HAVE_THREAD_NATIVE_ID :
1227+ self ._set_native_id ()
11981228 with _active_limbo_lock :
11991229 _active [self ._ident ] = self
12001230
0 commit comments