@@ -2860,6 +2860,7 @@ def test_py_thread_id(self):
2860
2860
# gh-112535: Test _Py_ThreadId(): make sure that thread identifiers
2861
2861
# in a few threads are unique
2862
2862
py_thread_id = _testinternalcapi .py_thread_id
2863
+ short_sleep = 0.010
2863
2864
2864
2865
class GetThreadId (threading .Thread ):
2865
2866
def __init__ (self ):
@@ -2873,6 +2874,8 @@ def run(self):
2873
2874
self .started_lock .set ()
2874
2875
self .get_lock .acquire ()
2875
2876
self .py_tid = py_thread_id ()
2877
+ time .sleep (short_sleep )
2878
+ self .py_tid2 = py_thread_id ()
2876
2879
2877
2880
nthread = 5
2878
2881
threads = [GetThreadId () for _ in range (nthread )]
@@ -2894,10 +2897,14 @@ def run(self):
2894
2897
for thread in threads :
2895
2898
thread .join ()
2896
2899
py_thread_ids .append (thread .py_tid )
2897
- for tid in py_thread_ids :
2898
- self .assertIsInstance (tid , int )
2900
+ # _PyThread_Id() should not change for a given thread.
2901
+ # For example, it should remain the same after a short sleep.
2902
+ self .assertEqual (thread .py_tid2 , thread .py_tid )
2899
2903
2900
2904
# make sure that all _Py_ThreadId() are unique
2905
+ for tid in py_thread_ids :
2906
+ self .assertIsInstance (tid , int )
2907
+ self .assertGreater (tid , 0 )
2901
2908
self .assertEqual (len (set (py_thread_ids )), len (py_thread_ids ),
2902
2909
py_thread_ids )
2903
2910
0 commit comments