blob: fb70abd17aff8b04ec2c77d5b4ceae0a1c4421c9 [file] [log] [blame]
Jeremy Hylton92bb6e72002-08-14 19:25:42 +00001"""Thread module emulating a subset of Java's threading model."""
Guido van Rossum7f5013a1998-04-09 22:01:42 +00002
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02003import os as _os
Fred Drakea8725952002-12-30 23:32:50 +00004import sys as _sys
Georg Brandl2067bfd2008-05-25 13:05:15 +00005import _thread
Kyle Stanleyb61b8182020-03-27 15:31:22 -04006import functools
Fred Drakea8725952002-12-30 23:32:50 +00007
Victor Stinnerae586492014-09-02 23:18:25 +02008from time import monotonic as _time
Antoine Pitrouc081c0c2011-07-15 22:12:24 +02009from _weakrefset import WeakSet
R David Murrayb186f1df2014-10-04 17:43:54 -040010from itertools import islice as _islice, count as _count
Raymond Hettingerec4b1742013-03-10 17:57:28 -070011try:
Raymond Hettingerec4b1742013-03-10 17:57:28 -070012 from _collections import deque as _deque
Brett Cannoncd171c82013-07-04 17:43:24 -040013except ImportError:
Raymond Hettingerec4b1742013-03-10 17:57:28 -070014 from collections import deque as _deque
Guido van Rossum7f5013a1998-04-09 22:01:42 +000015
Benjamin Petersonb3085c92008-09-01 23:09:31 +000016# Note regarding PEP 8 compliant names
17# This threading model was originally inspired by Java, and inherited
18# the convention of camelCase function and method names from that
Ezio Melotti30b9d5d2013-08-17 15:50:46 +030019# language. Those original names are not in any imminent danger of
Benjamin Petersonb3085c92008-09-01 23:09:31 +000020# being deprecated (even for Py3k),so this module provides them as an
21# alias for the PEP 8 compliant names
22# Note that using the new PEP 8 compliant names facilitates substitution
23# with the multiprocessing module, which doesn't provide the old
24# Java inspired names.
25
Victor Stinnerd12e7572019-05-21 12:44:57 +020026__all__ = ['get_ident', 'active_count', 'Condition', 'current_thread',
27 'enumerate', 'main_thread', 'TIMEOUT_MAX',
Martin Panter19e69c52015-11-14 12:46:42 +000028 'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
29 'Barrier', 'BrokenBarrierError', 'Timer', 'ThreadError',
Victor Stinnercd590a72019-05-28 00:39:52 +020030 'setprofile', 'settrace', 'local', 'stack_size',
Mario Corchero0001a1b2020-11-04 10:27:43 +010031 'excepthook', 'ExceptHookArgs', 'gettrace', 'getprofile']
Guido van Rossum7f5013a1998-04-09 22:01:42 +000032
Raymond Hettinger5cee47f2011-01-11 19:59:46 +000033# Rename some stuff so "from threading import *" is safe
Georg Brandl2067bfd2008-05-25 13:05:15 +000034_start_new_thread = _thread.start_new_thread
35_allocate_lock = _thread.allocate_lock
Antoine Pitrou7b476992013-09-07 23:38:37 +020036_set_sentinel = _thread._set_sentinel
Victor Stinner2a129742011-05-30 23:02:52 +020037get_ident = _thread.get_ident
Jake Teslerb121f632019-05-22 08:43:17 -070038try:
39 get_native_id = _thread.get_native_id
40 _HAVE_THREAD_NATIVE_ID = True
41 __all__.append('get_native_id')
42except AttributeError:
43 _HAVE_THREAD_NATIVE_ID = False
Georg Brandl2067bfd2008-05-25 13:05:15 +000044ThreadError = _thread.error
Antoine Pitrou434736a2009-11-10 18:46:01 +000045try:
46 _CRLock = _thread.RLock
47except AttributeError:
48 _CRLock = None
Antoine Pitrou7c3e5772010-04-14 15:44:10 +000049TIMEOUT_MAX = _thread.TIMEOUT_MAX
Georg Brandl2067bfd2008-05-25 13:05:15 +000050del _thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +000051
Guido van Rossum7f5013a1998-04-09 22:01:42 +000052
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000053# Support for profile and trace hooks
54
55_profile_hook = None
56_trace_hook = None
57
58def setprofile(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020059 """Set a profile function for all threads started from the threading module.
60
61 The func will be passed to sys.setprofile() for each thread, before its
62 run() method is called.
63
64 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000065 global _profile_hook
66 _profile_hook = func
Tim Petersd1b108b2003-06-29 17:24:17 +000067
Mario Corchero0001a1b2020-11-04 10:27:43 +010068def getprofile():
69 """Get the profiler function as set by threading.setprofile()."""
70 return _profile_hook
71
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000072def settrace(func):
Georg Brandlc30b59f2013-10-13 10:43:59 +020073 """Set a trace function for all threads started from the threading module.
74
75 The func will be passed to sys.settrace() for each thread, before its run()
76 method is called.
77
78 """
Jeremy Hyltonbfccb352003-06-29 16:58:41 +000079 global _trace_hook
80 _trace_hook = func
Guido van Rossum7f5013a1998-04-09 22:01:42 +000081
Mario Corchero0001a1b2020-11-04 10:27:43 +010082def gettrace():
83 """Get the trace function as set by threading.settrace()."""
84 return _trace_hook
85
Guido van Rossum7f5013a1998-04-09 22:01:42 +000086# Synchronization classes
87
88Lock = _allocate_lock
89
Victor Stinner135b6d82012-03-03 01:32:57 +010090def RLock(*args, **kwargs):
Georg Brandlc30b59f2013-10-13 10:43:59 +020091 """Factory function that returns a new reentrant lock.
92
93 A reentrant lock must be released by the thread that acquired it. Once a
94 thread has acquired a reentrant lock, the same thread may acquire it again
95 without blocking; the thread must release it once for each time it has
96 acquired it.
97
98 """
Victor Stinner135b6d82012-03-03 01:32:57 +010099 if _CRLock is None:
100 return _PyRLock(*args, **kwargs)
Antoine Pitrou434736a2009-11-10 18:46:01 +0000101 return _CRLock(*args, **kwargs)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000102
Victor Stinner135b6d82012-03-03 01:32:57 +0100103class _RLock:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200104 """This class implements reentrant lock objects.
105
106 A reentrant lock must be released by the thread that acquired it. Once a
107 thread has acquired a reentrant lock, the same thread may acquire it
108 again without blocking; the thread must release it once for each time it
109 has acquired it.
110
111 """
Tim Petersb90f89a2001-01-15 03:26:36 +0000112
Victor Stinner135b6d82012-03-03 01:32:57 +0100113 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000114 self._block = _allocate_lock()
115 self._owner = None
116 self._count = 0
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000117
118 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000119 owner = self._owner
Antoine Pitroub0872682009-11-09 16:08:16 +0000120 try:
121 owner = _active[owner].name
122 except KeyError:
123 pass
Raymond Hettinger62f4dad2014-05-25 18:22:35 -0700124 return "<%s %s.%s object owner=%r count=%d at %s>" % (
125 "locked" if self._block.locked() else "unlocked",
126 self.__class__.__module__,
127 self.__class__.__qualname__,
128 owner,
129 self._count,
130 hex(id(self))
131 )
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000132
Victor Stinner87255be2020-04-07 23:11:49 +0200133 def _at_fork_reinit(self):
134 self._block._at_fork_reinit()
135 self._owner = None
136 self._count = 0
137
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000138 def acquire(self, blocking=True, timeout=-1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200139 """Acquire a lock, blocking or non-blocking.
140
141 When invoked without arguments: if this thread already owns the lock,
142 increment the recursion level by one, and return immediately. Otherwise,
143 if another thread owns the lock, block until the lock is unlocked. Once
144 the lock is unlocked (not owned by any thread), then grab ownership, set
145 the recursion level to one, and return. If more than one thread is
146 blocked waiting until the lock is unlocked, only one at a time will be
147 able to grab ownership of the lock. There is no return value in this
148 case.
149
150 When invoked with the blocking argument set to true, do the same thing
151 as when called without arguments, and return true.
152
153 When invoked with the blocking argument set to false, do not block. If a
154 call without an argument would block, return false immediately;
155 otherwise, do the same thing as when called without arguments, and
156 return true.
157
158 When invoked with the floating-point timeout argument set to a positive
159 value, block for at most the number of seconds specified by timeout
160 and as long as the lock cannot be acquired. Return true if the lock has
161 been acquired, false if the timeout has elapsed.
162
163 """
Victor Stinner2a129742011-05-30 23:02:52 +0200164 me = get_ident()
Antoine Pitroub0872682009-11-09 16:08:16 +0000165 if self._owner == me:
Raymond Hettinger720da572013-03-10 15:13:35 -0700166 self._count += 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000167 return 1
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000168 rc = self._block.acquire(blocking, timeout)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000169 if rc:
Guido van Rossumd0648992007-08-20 19:25:41 +0000170 self._owner = me
171 self._count = 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000172 return rc
173
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000174 __enter__ = acquire
175
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000176 def release(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200177 """Release a lock, decrementing the recursion level.
178
179 If after the decrement it is zero, reset the lock to unlocked (not owned
180 by any thread), and if any other threads are blocked waiting for the
181 lock to become unlocked, allow exactly one of them to proceed. If after
182 the decrement the recursion level is still nonzero, the lock remains
183 locked and owned by the calling thread.
184
185 Only call this method when the calling thread owns the lock. A
186 RuntimeError is raised if this method is called when the lock is
187 unlocked.
188
189 There is no return value.
190
191 """
Victor Stinner2a129742011-05-30 23:02:52 +0200192 if self._owner != get_ident():
Georg Brandl495f7b52009-10-27 15:28:25 +0000193 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000194 self._count = count = self._count - 1
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000195 if not count:
Guido van Rossumd0648992007-08-20 19:25:41 +0000196 self._owner = None
197 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000198
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000199 def __exit__(self, t, v, tb):
200 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000201
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000202 # Internal methods used by condition variables
203
Guido van Rossum1bc535d2007-05-15 18:46:22 +0000204 def _acquire_restore(self, state):
Guido van Rossumd0648992007-08-20 19:25:41 +0000205 self._block.acquire()
206 self._count, self._owner = state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000207
208 def _release_save(self):
Victor Stinnerc2824d42011-04-24 23:41:33 +0200209 if self._count == 0:
210 raise RuntimeError("cannot release un-acquired lock")
Guido van Rossumd0648992007-08-20 19:25:41 +0000211 count = self._count
212 self._count = 0
213 owner = self._owner
214 self._owner = None
215 self._block.release()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000216 return (count, owner)
217
218 def _is_owned(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200219 return self._owner == get_ident()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000220
Antoine Pitrou434736a2009-11-10 18:46:01 +0000221_PyRLock = _RLock
222
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000223
Victor Stinner135b6d82012-03-03 01:32:57 +0100224class Condition:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200225 """Class that implements a condition variable.
226
227 A condition variable allows one or more threads to wait until they are
228 notified by another thread.
229
230 If the lock argument is given and not None, it must be a Lock or RLock
231 object, and it is used as the underlying lock. Otherwise, a new RLock object
232 is created and used as the underlying lock.
233
234 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000235
Victor Stinner135b6d82012-03-03 01:32:57 +0100236 def __init__(self, lock=None):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000237 if lock is None:
238 lock = RLock()
Guido van Rossumd0648992007-08-20 19:25:41 +0000239 self._lock = lock
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000240 # Export the lock's acquire() and release() methods
241 self.acquire = lock.acquire
242 self.release = lock.release
243 # If the lock defines _release_save() and/or _acquire_restore(),
244 # these override the default implementations (which just call
245 # release() and acquire() on the lock). Ditto for _is_owned().
246 try:
247 self._release_save = lock._release_save
248 except AttributeError:
249 pass
250 try:
251 self._acquire_restore = lock._acquire_restore
252 except AttributeError:
253 pass
254 try:
255 self._is_owned = lock._is_owned
256 except AttributeError:
257 pass
Raymond Hettingerec4b1742013-03-10 17:57:28 -0700258 self._waiters = _deque()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000259
Victor Stinner87255be2020-04-07 23:11:49 +0200260 def _at_fork_reinit(self):
261 self._lock._at_fork_reinit()
262 self._waiters.clear()
263
Thomas Wouters477c8d52006-05-27 19:21:47 +0000264 def __enter__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000265 return self._lock.__enter__()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000266
Thomas Wouters477c8d52006-05-27 19:21:47 +0000267 def __exit__(self, *args):
Guido van Rossumd0648992007-08-20 19:25:41 +0000268 return self._lock.__exit__(*args)
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000269
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000270 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000271 return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000272
273 def _release_save(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000274 self._lock.release() # No state to save
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000275
276 def _acquire_restore(self, x):
Guido van Rossumd0648992007-08-20 19:25:41 +0000277 self._lock.acquire() # Ignore saved state
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000278
279 def _is_owned(self):
Benjamin Peterson672b8032008-06-11 19:14:14 +0000280 # Return True if lock is owned by current_thread.
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300281 # This method is called only if _lock doesn't have _is_owned().
Serhiy Storchaka1f21eaa2019-09-01 12:16:51 +0300282 if self._lock.acquire(False):
Guido van Rossumd0648992007-08-20 19:25:41 +0000283 self._lock.release()
Tim Petersbc0e9102002-04-04 22:55:58 +0000284 return False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000285 else:
Tim Petersbc0e9102002-04-04 22:55:58 +0000286 return True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000287
288 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200289 """Wait until notified or until a timeout occurs.
290
291 If the calling thread has not acquired the lock when this method is
292 called, a RuntimeError is raised.
293
294 This method releases the underlying lock, and then blocks until it is
295 awakened by a notify() or notify_all() call for the same condition
296 variable in another thread, or until the optional timeout occurs. Once
297 awakened or timed out, it re-acquires the lock and returns.
298
299 When the timeout argument is present and not None, it should be a
300 floating point number specifying a timeout for the operation in seconds
301 (or fractions thereof).
302
303 When the underlying lock is an RLock, it is not released using its
304 release() method, since this may not actually unlock the lock when it
305 was acquired multiple times recursively. Instead, an internal interface
306 of the RLock class is used, which really unlocks it even when it has
307 been recursively acquired several times. Another internal interface is
308 then used to restore the recursion level when the lock is reacquired.
309
310 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000311 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000312 raise RuntimeError("cannot wait on un-acquired lock")
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000313 waiter = _allocate_lock()
314 waiter.acquire()
Guido van Rossumd0648992007-08-20 19:25:41 +0000315 self._waiters.append(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000316 saved_state = self._release_save()
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200317 gotit = False
Tim Petersc951bf92001-04-02 20:15:57 +0000318 try: # restore state no matter what (e.g., KeyboardInterrupt)
319 if timeout is None:
320 waiter.acquire()
Georg Brandlb9a43912010-10-28 09:03:20 +0000321 gotit = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000322 else:
Antoine Pitrou7c3e5772010-04-14 15:44:10 +0000323 if timeout > 0:
324 gotit = waiter.acquire(True, timeout)
325 else:
326 gotit = waiter.acquire(False)
Georg Brandlb9a43912010-10-28 09:03:20 +0000327 return gotit
Tim Petersc951bf92001-04-02 20:15:57 +0000328 finally:
329 self._acquire_restore(saved_state)
Antoine Pitroua64b92e2014-08-29 23:26:36 +0200330 if not gotit:
331 try:
332 self._waiters.remove(waiter)
333 except ValueError:
334 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000335
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000336 def wait_for(self, predicate, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200337 """Wait until a condition evaluates to True.
338
339 predicate should be a callable which result will be interpreted as a
340 boolean value. A timeout may be provided giving the maximum time to
341 wait.
342
343 """
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000344 endtime = None
345 waittime = timeout
346 result = predicate()
347 while not result:
348 if waittime is not None:
349 if endtime is None:
350 endtime = _time() + waittime
351 else:
352 waittime = endtime - _time()
353 if waittime <= 0:
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000354 break
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000355 self.wait(waittime)
356 result = predicate()
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000357 return result
358
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000359 def notify(self, n=1):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200360 """Wake up one or more threads waiting on this condition, if any.
361
362 If the calling thread has not acquired the lock when this method is
363 called, a RuntimeError is raised.
364
365 This method wakes up at most n of the threads waiting for the condition
366 variable; it is a no-op if no threads are waiting.
367
368 """
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000369 if not self._is_owned():
Georg Brandl495f7b52009-10-27 15:28:25 +0000370 raise RuntimeError("cannot notify on un-acquired lock")
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700371 all_waiters = self._waiters
372 waiters_to_notify = _deque(_islice(all_waiters, n))
373 if not waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000374 return
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700375 for waiter in waiters_to_notify:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000376 waiter.release()
377 try:
Raymond Hettingerb65e5792013-03-10 20:34:16 -0700378 all_waiters.remove(waiter)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000379 except ValueError:
380 pass
381
Benjamin Peterson672b8032008-06-11 19:14:14 +0000382 def notify_all(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200383 """Wake up all threads waiting on this condition.
384
385 If the calling thread has not acquired the lock when this method
386 is called, a RuntimeError is raised.
387
388 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000389 self.notify(len(self._waiters))
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000390
Jelle Zijlstra9825bdf2021-04-12 01:42:53 -0700391 def notifyAll(self):
392 """Wake up all threads waiting on this condition.
393
394 This method is deprecated, use notify_all() instead.
395
396 """
397 import warnings
398 warnings.warn('notifyAll() is deprecated, use notify_all() instead',
399 DeprecationWarning, stacklevel=2)
400 self.notify_all()
Benjamin Petersonb3085c92008-09-01 23:09:31 +0000401
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000402
Victor Stinner135b6d82012-03-03 01:32:57 +0100403class Semaphore:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200404 """This class implements semaphore objects.
405
406 Semaphores manage a counter representing the number of release() calls minus
407 the number of acquire() calls, plus an initial value. The acquire() method
408 blocks if necessary until it can return without making the counter
409 negative. If not given, value defaults to 1.
410
411 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000412
Andrew M. Kuchling39d3bfc2000-02-29 00:10:24 +0000413 # After Tim Peters' semaphore class, but not quite the same (no maximum)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000414
Victor Stinner135b6d82012-03-03 01:32:57 +0100415 def __init__(self, value=1):
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000416 if value < 0:
417 raise ValueError("semaphore initial value must be >= 0")
Guido van Rossumd0648992007-08-20 19:25:41 +0000418 self._cond = Condition(Lock())
419 self._value = value
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000420
Antoine Pitrou0454af92010-04-17 23:51:58 +0000421 def acquire(self, blocking=True, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200422 """Acquire a semaphore, decrementing the internal counter by one.
423
424 When invoked without arguments: if the internal counter is larger than
425 zero on entry, decrement it by one and return immediately. If it is zero
426 on entry, block, waiting until some other thread has called release() to
427 make it larger than zero. This is done with proper interlocking so that
428 if multiple acquire() calls are blocked, release() will wake exactly one
429 of them up. The implementation may pick one at random, so the order in
430 which blocked threads are awakened should not be relied on. There is no
431 return value in this case.
432
433 When invoked with blocking set to true, do the same thing as when called
434 without arguments, and return true.
435
436 When invoked with blocking set to false, do not block. If a call without
437 an argument would block, return false immediately; otherwise, do the
438 same thing as when called without arguments, and return true.
439
440 When invoked with a timeout other than None, it will block for at
441 most timeout seconds. If acquire does not complete successfully in
442 that interval, return false. Return true otherwise.
443
444 """
Antoine Pitrou0454af92010-04-17 23:51:58 +0000445 if not blocking and timeout is not None:
446 raise ValueError("can't specify timeout for non-blocking acquire")
Guido van Rossum8ca162f2002-04-07 06:36:23 +0000447 rc = False
Antoine Pitrou0454af92010-04-17 23:51:58 +0000448 endtime = None
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300449 with self._cond:
450 while self._value == 0:
451 if not blocking:
452 break
453 if timeout is not None:
454 if endtime is None:
455 endtime = _time() + timeout
456 else:
457 timeout = endtime - _time()
458 if timeout <= 0:
459 break
460 self._cond.wait(timeout)
461 else:
Serhiy Storchakab00b5962013-04-22 22:54:16 +0300462 self._value -= 1
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300463 rc = True
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000464 return rc
465
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000466 __enter__ = acquire
467
Raymond Hettinger35f63012019-08-29 01:45:19 -0700468 def release(self, n=1):
469 """Release a semaphore, incrementing the internal counter by one or more.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200470
471 When the counter is zero on entry and another thread is waiting for it
472 to become larger than zero again, wake up that thread.
473
474 """
Raymond Hettinger35f63012019-08-29 01:45:19 -0700475 if n < 1:
476 raise ValueError('n must be one or more')
Serhiy Storchaka81a58552013-04-22 22:51:43 +0300477 with self._cond:
Raymond Hettinger35f63012019-08-29 01:45:19 -0700478 self._value += n
479 for i in range(n):
480 self._cond.notify()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000481
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000482 def __exit__(self, t, v, tb):
483 self.release()
Guido van Rossum1a5e21e2006-02-28 21:57:43 +0000484
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000485
Éric Araujo0cdd4452011-07-28 00:28:28 +0200486class BoundedSemaphore(Semaphore):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200487 """Implements a bounded semaphore.
488
489 A bounded semaphore checks to make sure its current value doesn't exceed its
490 initial value. If it does, ValueError is raised. In most situations
491 semaphores are used to guard resources with limited capacity.
492
493 If the semaphore is released too many times it's a sign of a bug. If not
494 given, value defaults to 1.
495
496 Like regular semaphores, bounded semaphores manage a counter representing
497 the number of release() calls minus the number of acquire() calls, plus an
498 initial value. The acquire() method blocks if necessary until it can return
499 without making the counter negative. If not given, value defaults to 1.
500
501 """
502
Victor Stinner135b6d82012-03-03 01:32:57 +0100503 def __init__(self, value=1):
504 Semaphore.__init__(self, value)
Skip Montanaroe428bb72001-08-20 20:27:58 +0000505 self._initial_value = value
506
Raymond Hettinger35f63012019-08-29 01:45:19 -0700507 def release(self, n=1):
508 """Release a semaphore, incrementing the internal counter by one or more.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200509
510 When the counter is zero on entry and another thread is waiting for it
511 to become larger than zero again, wake up that thread.
512
513 If the number of releases exceeds the number of acquires,
514 raise a ValueError.
515
516 """
Raymond Hettinger35f63012019-08-29 01:45:19 -0700517 if n < 1:
518 raise ValueError('n must be one or more')
Tim Peters7634e1c2013-10-08 20:55:51 -0500519 with self._cond:
Raymond Hettinger35f63012019-08-29 01:45:19 -0700520 if self._value + n > self._initial_value:
Tim Peters7634e1c2013-10-08 20:55:51 -0500521 raise ValueError("Semaphore released too many times")
Raymond Hettinger35f63012019-08-29 01:45:19 -0700522 self._value += n
523 for i in range(n):
524 self._cond.notify()
Skip Montanaroe428bb72001-08-20 20:27:58 +0000525
526
Victor Stinner135b6d82012-03-03 01:32:57 +0100527class Event:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200528 """Class implementing event objects.
529
530 Events manage a flag that can be set to true with the set() method and reset
531 to false with the clear() method. The wait() method blocks until the flag is
532 true. The flag is initially false.
533
534 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000535
536 # After Tim Peters' event class (without is_posted())
537
Victor Stinner135b6d82012-03-03 01:32:57 +0100538 def __init__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000539 self._cond = Condition(Lock())
540 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000541
Victor Stinner87255be2020-04-07 23:11:49 +0200542 def _at_fork_reinit(self):
543 # Private method called by Thread._reset_internal_locks()
544 self._cond._at_fork_reinit()
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000545
Benjamin Peterson672b8032008-06-11 19:14:14 +0000546 def is_set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200547 """Return true if and only if the internal flag is true."""
Guido van Rossumd0648992007-08-20 19:25:41 +0000548 return self._flag
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000549
Jelle Zijlstra9825bdf2021-04-12 01:42:53 -0700550 def isSet(self):
551 """Return true if and only if the internal flag is true.
552
553 This method is deprecated, use notify_all() instead.
554
555 """
556 import warnings
557 warnings.warn('isSet() is deprecated, use is_set() instead',
558 DeprecationWarning, stacklevel=2)
559 return self.is_set()
Benjamin Petersonf0923f52008-08-18 22:10:13 +0000560
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000561 def set(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200562 """Set the internal flag to true.
563
564 All threads waiting for it to become true are awakened. Threads
565 that call wait() once the flag is true will not block at all.
566
567 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700568 with self._cond:
Guido van Rossumd0648992007-08-20 19:25:41 +0000569 self._flag = True
Benjamin Peterson672b8032008-06-11 19:14:14 +0000570 self._cond.notify_all()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000571
572 def clear(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200573 """Reset the internal flag to false.
574
575 Subsequently, threads calling wait() will block until set() is called to
576 set the internal flag to true again.
577
578 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700579 with self._cond:
Guido van Rossumd0648992007-08-20 19:25:41 +0000580 self._flag = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000581
582 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200583 """Block until the internal flag is true.
584
585 If the internal flag is true on entry, return immediately. Otherwise,
586 block until another thread calls set() to set the flag to true, or until
587 the optional timeout occurs.
588
589 When the timeout argument is present and not None, it should be a
590 floating point number specifying a timeout for the operation in seconds
591 (or fractions thereof).
592
593 This method returns the internal flag on exit, so it will always return
594 True except if a timeout is given and the operation times out.
595
596 """
Benjamin Peterson414918a2015-10-10 19:34:46 -0700597 with self._cond:
Charles-François Natalided03482012-01-07 18:24:56 +0100598 signaled = self._flag
599 if not signaled:
600 signaled = self._cond.wait(timeout)
601 return signaled
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000602
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000603
604# A barrier class. Inspired in part by the pthread_barrier_* api and
605# the CyclicBarrier class from Java. See
606# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
607# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
608# CyclicBarrier.html
609# for information.
610# We maintain two main states, 'filling' and 'draining' enabling the barrier
611# to be cyclic. Threads are not allowed into it until it has fully drained
612# since the previous cycle. In addition, a 'resetting' state exists which is
613# similar to 'draining' except that threads leave with a BrokenBarrierError,
Ezio Melottie130a522011-10-19 10:58:56 +0300614# and a 'broken' state in which all threads get the exception.
Victor Stinner135b6d82012-03-03 01:32:57 +0100615class Barrier:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200616 """Implements a Barrier.
617
618 Useful for synchronizing a fixed number of threads at known synchronization
Carl Bordum Hansen62fa51f2019-03-09 18:38:05 +0100619 points. Threads block on 'wait()' and are simultaneously awoken once they
620 have all made that call.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200621
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000622 """
Georg Brandlc30b59f2013-10-13 10:43:59 +0200623
Victor Stinner135b6d82012-03-03 01:32:57 +0100624 def __init__(self, parties, action=None, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200625 """Create a barrier, initialised to 'parties' threads.
626
627 'action' is a callable which, when supplied, will be called by one of
628 the threads after they have all entered the barrier and just prior to
Carl Bordum Hansen62fa51f2019-03-09 18:38:05 +0100629 releasing them all. If a 'timeout' is provided, it is used as the
Georg Brandlc30b59f2013-10-13 10:43:59 +0200630 default for all subsequent 'wait()' calls.
631
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000632 """
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000633 self._cond = Condition(Lock())
634 self._action = action
635 self._timeout = timeout
636 self._parties = parties
637 self._state = 0 #0 filling, 1, draining, -1 resetting, -2 broken
638 self._count = 0
639
640 def wait(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200641 """Wait for the barrier.
642
643 When the specified number of threads have started waiting, they are all
644 simultaneously awoken. If an 'action' was provided for the barrier, one
645 of the threads will have executed that callback prior to returning.
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000646 Returns an individual index number from 0 to 'parties-1'.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200647
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000648 """
649 if timeout is None:
650 timeout = self._timeout
651 with self._cond:
652 self._enter() # Block while the barrier drains.
653 index = self._count
654 self._count += 1
655 try:
656 if index + 1 == self._parties:
657 # We release the barrier
658 self._release()
659 else:
660 # We wait until someone releases us
661 self._wait(timeout)
662 return index
663 finally:
664 self._count -= 1
665 # Wake up any threads waiting for barrier to drain.
666 self._exit()
667
668 # Block until the barrier is ready for us, or raise an exception
669 # if it is broken.
670 def _enter(self):
671 while self._state in (-1, 1):
672 # It is draining or resetting, wait until done
673 self._cond.wait()
674 #see if the barrier is in a broken state
675 if self._state < 0:
676 raise BrokenBarrierError
677 assert self._state == 0
678
679 # Optionally run the 'action' and release the threads waiting
680 # in the barrier.
681 def _release(self):
682 try:
683 if self._action:
684 self._action()
685 # enter draining state
686 self._state = 1
687 self._cond.notify_all()
688 except:
689 #an exception during the _action handler. Break and reraise
690 self._break()
691 raise
692
Martin Panter69332c12016-08-04 13:07:31 +0000693 # Wait in the barrier until we are released. Raise an exception
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000694 # if the barrier is reset or broken.
695 def _wait(self, timeout):
Kristján Valur Jónsson63315202010-11-18 12:46:39 +0000696 if not self._cond.wait_for(lambda : self._state != 0, timeout):
697 #timed out. Break the barrier
698 self._break()
699 raise BrokenBarrierError
700 if self._state < 0:
701 raise BrokenBarrierError
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000702 assert self._state == 1
703
704 # If we are the last thread to exit the barrier, signal any threads
705 # waiting for the barrier to drain.
706 def _exit(self):
707 if self._count == 0:
708 if self._state in (-1, 1):
709 #resetting or draining
710 self._state = 0
711 self._cond.notify_all()
712
713 def reset(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200714 """Reset the barrier to the initial state.
715
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000716 Any threads currently waiting will get the BrokenBarrier exception
717 raised.
Georg Brandlc30b59f2013-10-13 10:43:59 +0200718
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000719 """
720 with self._cond:
721 if self._count > 0:
722 if self._state == 0:
723 #reset the barrier, waking up threads
724 self._state = -1
725 elif self._state == -2:
726 #was broken, set it to reset state
727 #which clears when the last thread exits
728 self._state = -1
729 else:
730 self._state = 0
731 self._cond.notify_all()
732
733 def abort(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200734 """Place the barrier into a 'broken' state.
735
736 Useful in case of error. Any currently waiting threads and threads
737 attempting to 'wait()' will have BrokenBarrierError raised.
738
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000739 """
740 with self._cond:
741 self._break()
742
743 def _break(self):
744 # An internal error was detected. The barrier is set to
745 # a broken state all parties awakened.
746 self._state = -2
747 self._cond.notify_all()
748
749 @property
750 def parties(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200751 """Return the number of threads required to trip the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000752 return self._parties
753
754 @property
755 def n_waiting(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200756 """Return the number of threads currently waiting at the barrier."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000757 # We don't need synchronization here since this is an ephemeral result
758 # anyway. It returns the correct value in the steady state.
759 if self._state == 0:
760 return self._count
761 return 0
762
763 @property
764 def broken(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200765 """Return True if the barrier is in a broken state."""
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000766 return self._state == -2
767
Georg Brandlc30b59f2013-10-13 10:43:59 +0200768# exception raised by the Barrier class
769class BrokenBarrierError(RuntimeError):
770 pass
Kristján Valur Jónsson3be00032010-10-28 09:43:10 +0000771
772
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000773# Helper to generate new thread names
Victor Stinner98c16c92020-09-23 23:21:19 +0200774_counter = _count(1).__next__
775def _newname(name_template):
776 return name_template % _counter()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000777
778# Active thread administration
779_active_limbo_lock = _allocate_lock()
Tim Peters711906e2005-01-08 07:30:42 +0000780_active = {} # maps thread id to Thread object
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000781_limbo = {}
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200782_dangling = WeakSet()
Victor Stinner468e5fe2019-06-13 01:30:17 +0200783# Set of Thread._tstate_lock locks of non-daemon threads used by _shutdown()
784# to wait until all Python thread states get deleted:
785# see Thread._set_tstate_lock().
786_shutdown_locks_lock = _allocate_lock()
787_shutdown_locks = set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000788
789# Main class for threads
790
Victor Stinner135b6d82012-03-03 01:32:57 +0100791class Thread:
Georg Brandlc30b59f2013-10-13 10:43:59 +0200792 """A class that represents a thread of control.
793
794 This class can be safely subclassed in a limited fashion. There are two ways
795 to specify the activity: by passing a callable object to the constructor, or
796 by overriding the run() method in a subclass.
797
798 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000799
Serhiy Storchaka52005c22014-09-21 22:08:13 +0300800 _initialized = False
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000801
802 def __init__(self, group=None, target=None, name=None,
Victor Stinner135b6d82012-03-03 01:32:57 +0100803 args=(), kwargs=None, *, daemon=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200804 """This constructor should always be called with keyword arguments. Arguments are:
805
806 *group* should be None; reserved for future extension when a ThreadGroup
807 class is implemented.
808
809 *target* is the callable object to be invoked by the run()
810 method. Defaults to None, meaning nothing is called.
811
812 *name* is the thread name. By default, a unique name is constructed of
813 the form "Thread-N" where N is a small decimal number.
814
815 *args* is the argument tuple for the target invocation. Defaults to ().
816
817 *kwargs* is a dictionary of keyword arguments for the target
818 invocation. Defaults to {}.
819
820 If a subclass overrides the constructor, it must make sure to invoke
821 the base class constructor (Thread.__init__()) before doing anything
822 else to the thread.
823
824 """
Guido van Rossum5a43e1a1998-06-09 19:04:26 +0000825 assert group is None, "group argument must be None for now"
Georg Brandla4a8b822005-07-15 09:13:21 +0000826 if kwargs is None:
827 kwargs = {}
Victor Stinner98c16c92020-09-23 23:21:19 +0200828 if name:
829 name = str(name)
830 else:
831 name = _newname("Thread-%d")
832 if target is not None:
833 try:
834 target_name = target.__name__
835 name += f" ({target_name})"
836 except AttributeError:
837 pass
838
Guido van Rossumd0648992007-08-20 19:25:41 +0000839 self._target = target
Victor Stinner98c16c92020-09-23 23:21:19 +0200840 self._name = name
Guido van Rossumd0648992007-08-20 19:25:41 +0000841 self._args = args
842 self._kwargs = kwargs
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +0000843 if daemon is not None:
844 self._daemonic = daemon
845 else:
846 self._daemonic = current_thread().daemon
Georg Brandl0c77a822008-06-10 16:37:50 +0000847 self._ident = None
Jake Teslerb121f632019-05-22 08:43:17 -0700848 if _HAVE_THREAD_NATIVE_ID:
849 self._native_id = None
Antoine Pitrou7b476992013-09-07 23:38:37 +0200850 self._tstate_lock = None
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000851 self._started = Event()
Tim Petersc363a232013-09-08 18:44:40 -0500852 self._is_stopped = False
Guido van Rossumd0648992007-08-20 19:25:41 +0000853 self._initialized = True
Victor Stinnercd590a72019-05-28 00:39:52 +0200854 # Copy of sys.stderr used by self._invoke_excepthook()
Guido van Rossumd0648992007-08-20 19:25:41 +0000855 self._stderr = _sys.stderr
Victor Stinnercd590a72019-05-28 00:39:52 +0200856 self._invoke_excepthook = _make_invoke_excepthook()
Antoine Pitrou5da7e792013-09-08 13:19:06 +0200857 # For debugging and _after_fork()
Antoine Pitrouc081c0c2011-07-15 22:12:24 +0200858 _dangling.add(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000859
Antoine Pitrou7b476992013-09-07 23:38:37 +0200860 def _reset_internal_locks(self, is_alive):
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000861 # private! Called by _after_fork() to reset our internal locks as
862 # they may be in an invalid state leading to a deadlock or crash.
Victor Stinner87255be2020-04-07 23:11:49 +0200863 self._started._at_fork_reinit()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200864 if is_alive:
Victor Stinner5909a492020-11-16 15:20:34 +0100865 # bpo-42350: If the fork happens when the thread is already stopped
866 # (ex: after threading._shutdown() has been called), _tstate_lock
867 # is None. Do nothing in this case.
868 if self._tstate_lock is not None:
869 self._tstate_lock._at_fork_reinit()
870 self._tstate_lock.acquire()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200871 else:
872 # The thread isn't alive after fork: it doesn't have a tstate
873 # anymore.
Tim Petersb5e9ac92013-09-09 14:41:50 -0500874 self._is_stopped = True
Antoine Pitrou7b476992013-09-07 23:38:37 +0200875 self._tstate_lock = None
Gregory P. Smith9bd4a242011-01-04 18:33:38 +0000876
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000877 def __repr__(self):
Guido van Rossumd0648992007-08-20 19:25:41 +0000878 assert self._initialized, "Thread.__init__() was not called"
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000879 status = "initial"
Benjamin Peterson672b8032008-06-11 19:14:14 +0000880 if self._started.is_set():
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000881 status = "started"
Tim Peters72460fa2013-09-09 18:48:24 -0500882 self.is_alive() # easy way to get ._is_stopped set when appropriate
Tim Petersc363a232013-09-08 18:44:40 -0500883 if self._is_stopped:
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000884 status = "stopped"
Guido van Rossumd0648992007-08-20 19:25:41 +0000885 if self._daemonic:
Georg Brandl0c77a822008-06-10 16:37:50 +0000886 status += " daemon"
887 if self._ident is not None:
888 status += " %s" % self._ident
Guido van Rossumd0648992007-08-20 19:25:41 +0000889 return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000890
891 def start(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200892 """Start the thread's activity.
893
894 It must be called at most once per thread object. It arranges for the
895 object's run() method to be invoked in a separate thread of control.
896
897 This method will raise a RuntimeError if called more than once on the
898 same thread object.
899
900 """
Guido van Rossumd0648992007-08-20 19:25:41 +0000901 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +0000902 raise RuntimeError("thread.__init__() not called")
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000903
Benjamin Peterson672b8032008-06-11 19:14:14 +0000904 if self._started.is_set():
Senthil Kumaranfdd4d0f2010-04-06 03:30:18 +0000905 raise RuntimeError("threads can only be started once")
Victor Stinner066e5b12019-06-14 18:55:22 +0200906
Benjamin Petersond23f8222009-04-05 19:13:16 +0000907 with _active_limbo_lock:
908 _limbo[self] = self
Gregory P. Smith3fdd9642010-02-28 18:57:46 +0000909 try:
910 _start_new_thread(self._bootstrap, ())
911 except Exception:
912 with _active_limbo_lock:
913 del _limbo[self]
914 raise
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000915 self._started.wait()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000916
917 def run(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +0200918 """Method representing the thread's activity.
919
920 You may override this method in a subclass. The standard run() method
921 invokes the callable object passed to the object's constructor as the
922 target argument, if any, with sequential and keyword arguments taken
923 from the args and kwargs arguments, respectively.
924
925 """
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000926 try:
BarneyStratford01c4fdd2021-02-02 20:24:24 +0000927 if self._target is not None:
Christian Heimesd3eb5a152008-02-24 00:38:49 +0000928 self._target(*self._args, **self._kwargs)
929 finally:
930 # Avoid a refcycle if the thread is running a function with
931 # an argument that has a member that points to the thread.
932 del self._target, self._args, self._kwargs
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000933
Guido van Rossumd0648992007-08-20 19:25:41 +0000934 def _bootstrap(self):
Guido van Rossum61e21b52007-08-20 19:06:03 +0000935 # Wrapper around the real bootstrap code that ignores
936 # exceptions during interpreter cleanup. Those typically
937 # happen when a daemon thread wakes up at an unfortunate
938 # moment, finds the world around it destroyed, and raises some
939 # random exception *** while trying to report the exception in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000940 # _bootstrap_inner() below ***. Those random exceptions
Guido van Rossum61e21b52007-08-20 19:06:03 +0000941 # don't help anybody, and they confuse users, so we suppress
942 # them. We suppress them only when it appears that the world
943 # indeed has already been destroyed, so that exceptions in
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000944 # _bootstrap_inner() during normal business hours are properly
Guido van Rossum61e21b52007-08-20 19:06:03 +0000945 # reported. Also, we only suppress them for daemonic threads;
946 # if a non-daemonic encounters this, something else is wrong.
947 try:
Guido van Rossumd0648992007-08-20 19:25:41 +0000948 self._bootstrap_inner()
Guido van Rossum61e21b52007-08-20 19:06:03 +0000949 except:
Guido van Rossumd0648992007-08-20 19:25:41 +0000950 if self._daemonic and _sys is None:
Guido van Rossum61e21b52007-08-20 19:06:03 +0000951 return
952 raise
953
Benjamin Petersond23f8222009-04-05 19:13:16 +0000954 def _set_ident(self):
Victor Stinner2a129742011-05-30 23:02:52 +0200955 self._ident = get_ident()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000956
Jake Teslerb121f632019-05-22 08:43:17 -0700957 if _HAVE_THREAD_NATIVE_ID:
958 def _set_native_id(self):
959 self._native_id = get_native_id()
960
Antoine Pitrou7b476992013-09-07 23:38:37 +0200961 def _set_tstate_lock(self):
962 """
963 Set a lock object which will be released by the interpreter when
964 the underlying thread state (see pystate.h) gets deleted.
965 """
966 self._tstate_lock = _set_sentinel()
967 self._tstate_lock.acquire()
968
Victor Stinner468e5fe2019-06-13 01:30:17 +0200969 if not self.daemon:
970 with _shutdown_locks_lock:
971 _shutdown_locks.add(self._tstate_lock)
972
Guido van Rossumd0648992007-08-20 19:25:41 +0000973 def _bootstrap_inner(self):
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000974 try:
Benjamin Petersond23f8222009-04-05 19:13:16 +0000975 self._set_ident()
Antoine Pitrou7b476992013-09-07 23:38:37 +0200976 self._set_tstate_lock()
Jake Teslerb121f632019-05-22 08:43:17 -0700977 if _HAVE_THREAD_NATIVE_ID:
978 self._set_native_id()
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000979 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +0000980 with _active_limbo_lock:
981 _active[self._ident] = self
982 del _limbo[self]
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000983
984 if _trace_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000985 _sys.settrace(_trace_hook)
986 if _profile_hook:
Jeremy Hyltonbfccb352003-06-29 16:58:41 +0000987 _sys.setprofile(_profile_hook)
Tim Petersd1b108b2003-06-29 17:24:17 +0000988
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000989 try:
990 self.run()
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000991 except:
Victor Stinnercd590a72019-05-28 00:39:52 +0200992 self._invoke_excepthook(self)
Guido van Rossum7f5013a1998-04-09 22:01:42 +0000993 finally:
Christian Heimes1af737c2008-01-23 08:24:23 +0000994 with _active_limbo_lock:
Christian Heimes1af737c2008-01-23 08:24:23 +0000995 try:
Georg Brandl0c77a822008-06-10 16:37:50 +0000996 # We don't call self._delete() because it also
Christian Heimes1af737c2008-01-23 08:24:23 +0000997 # grabs _active_limbo_lock.
Victor Stinner2a129742011-05-30 23:02:52 +0200998 del _active[get_ident()]
Christian Heimes1af737c2008-01-23 08:24:23 +0000999 except:
1000 pass
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001001
Guido van Rossumd0648992007-08-20 19:25:41 +00001002 def _stop(self):
Tim Petersb5e9ac92013-09-09 14:41:50 -05001003 # After calling ._stop(), .is_alive() returns False and .join() returns
1004 # immediately. ._tstate_lock must be released before calling ._stop().
1005 #
1006 # Normal case: C code at the end of the thread's life
1007 # (release_sentinel in _threadmodule.c) releases ._tstate_lock, and
1008 # that's detected by our ._wait_for_tstate_lock(), called by .join()
1009 # and .is_alive(). Any number of threads _may_ call ._stop()
1010 # simultaneously (for example, if multiple threads are blocked in
1011 # .join() calls), and they're not serialized. That's harmless -
1012 # they'll just make redundant rebindings of ._is_stopped and
1013 # ._tstate_lock. Obscure: we rebind ._tstate_lock last so that the
1014 # "assert self._is_stopped" in ._wait_for_tstate_lock() always works
1015 # (the assert is executed only if ._tstate_lock is None).
1016 #
1017 # Special case: _main_thread releases ._tstate_lock via this
1018 # module's _shutdown() function.
1019 lock = self._tstate_lock
1020 if lock is not None:
1021 assert not lock.locked()
Tim Peters78755232013-09-09 13:47:16 -05001022 self._is_stopped = True
1023 self._tstate_lock = None
Victor Stinner468e5fe2019-06-13 01:30:17 +02001024 if not self.daemon:
1025 with _shutdown_locks_lock:
Victor Stinner6f75c872019-06-13 12:06:24 +02001026 _shutdown_locks.discard(lock)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001027
Guido van Rossumd0648992007-08-20 19:25:41 +00001028 def _delete(self):
Tim Peters21429932004-07-21 03:36:52 +00001029 "Remove current thread from the dict of currently running threads."
Antoine Pitroua6a4dc82017-09-07 18:56:24 +02001030 with _active_limbo_lock:
1031 del _active[get_ident()]
1032 # There must not be any python code between the previous line
1033 # and after the lock is released. Otherwise a tracing function
1034 # could try to acquire the lock again in the same thread, (in
1035 # current_thread()), and would block.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001036
1037 def join(self, timeout=None):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001038 """Wait until the thread terminates.
1039
1040 This blocks the calling thread until the thread whose join() method is
1041 called terminates -- either normally or through an unhandled exception
1042 or until the optional timeout occurs.
1043
1044 When the timeout argument is present and not None, it should be a
1045 floating point number specifying a timeout for the operation in seconds
1046 (or fractions thereof). As join() always returns None, you must call
Dong-hee Na36d9e9a2019-01-18 18:50:47 +09001047 is_alive() after join() to decide whether a timeout happened -- if the
Georg Brandlc30b59f2013-10-13 10:43:59 +02001048 thread is still alive, the join() call timed out.
1049
1050 When the timeout argument is not present or None, the operation will
1051 block until the thread terminates.
1052
1053 A thread can be join()ed many times.
1054
1055 join() raises a RuntimeError if an attempt is made to join the current
1056 thread as that would cause a deadlock. It is also an error to join() a
1057 thread before it has been started and attempts to do so raises the same
1058 exception.
1059
1060 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001061 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001062 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001063 if not self._started.is_set():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001064 raise RuntimeError("cannot join thread before it is started")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001065 if self is current_thread():
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001066 raise RuntimeError("cannot join current thread")
Tim Peterse5bb0bf2013-10-25 20:46:51 -05001067
Tim Petersc363a232013-09-08 18:44:40 -05001068 if timeout is None:
1069 self._wait_for_tstate_lock()
Tim Peters7bad39f2013-10-25 22:33:52 -05001070 else:
1071 # the behavior of a negative timeout isn't documented, but
Tim Petersa577f1e2013-10-26 11:56:16 -05001072 # historically .join(timeout=x) for x<0 has acted as if timeout=0
Tim Peters7bad39f2013-10-25 22:33:52 -05001073 self._wait_for_tstate_lock(timeout=max(timeout, 0))
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001074
Tim Petersc363a232013-09-08 18:44:40 -05001075 def _wait_for_tstate_lock(self, block=True, timeout=-1):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001076 # Issue #18808: wait for the thread state to be gone.
Tim Petersc363a232013-09-08 18:44:40 -05001077 # At the end of the thread's life, after all knowledge of the thread
1078 # is removed from C data structures, C code releases our _tstate_lock.
Martin Panter46f50722016-05-26 05:35:26 +00001079 # This method passes its arguments to _tstate_lock.acquire().
Tim Petersc363a232013-09-08 18:44:40 -05001080 # If the lock is acquired, the C code is done, and self._stop() is
1081 # called. That sets ._is_stopped to True, and ._tstate_lock to None.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001082 lock = self._tstate_lock
Tim Petersc363a232013-09-08 18:44:40 -05001083 if lock is None: # already determined that the C code is done
1084 assert self._is_stopped
1085 elif lock.acquire(block, timeout):
Antoine Pitrou7b476992013-09-07 23:38:37 +02001086 lock.release()
Tim Petersc363a232013-09-08 18:44:40 -05001087 self._stop()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001088
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001089 @property
1090 def name(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001091 """A string used for identification purposes only.
1092
1093 It has no semantics. Multiple threads may be given the same name. The
1094 initial name is set by the constructor.
1095
1096 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001097 assert self._initialized, "Thread.__init__() not called"
1098 return self._name
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001099
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001100 @name.setter
1101 def name(self, name):
Guido van Rossumd0648992007-08-20 19:25:41 +00001102 assert self._initialized, "Thread.__init__() not called"
1103 self._name = str(name)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001104
Benjamin Peterson773c17b2008-08-18 16:45:31 +00001105 @property
1106 def ident(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001107 """Thread identifier of this thread or None if it has not been started.
1108
Skip Montanaro56343312018-05-18 13:38:36 -05001109 This is a nonzero integer. See the get_ident() function. Thread
Georg Brandlc30b59f2013-10-13 10:43:59 +02001110 identifiers may be recycled when a thread exits and another thread is
1111 created. The identifier is available even after the thread has exited.
1112
1113 """
Georg Brandl0c77a822008-06-10 16:37:50 +00001114 assert self._initialized, "Thread.__init__() not called"
1115 return self._ident
1116
Jake Teslerb121f632019-05-22 08:43:17 -07001117 if _HAVE_THREAD_NATIVE_ID:
1118 @property
1119 def native_id(self):
1120 """Native integral thread ID of this thread, or None if it has not been started.
1121
1122 This is a non-negative integer. See the get_native_id() function.
1123 This represents the Thread ID as reported by the kernel.
1124
1125 """
1126 assert self._initialized, "Thread.__init__() not called"
1127 return self._native_id
1128
Benjamin Peterson672b8032008-06-11 19:14:14 +00001129 def is_alive(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001130 """Return whether the thread is alive.
1131
1132 This method returns True just before the run() method starts until just
Miss Islington (bot)7bef7a12021-05-11 11:19:27 -07001133 after the run() method terminates. See also the module function
1134 enumerate().
Georg Brandlc30b59f2013-10-13 10:43:59 +02001135
1136 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001137 assert self._initialized, "Thread.__init__() not called"
Tim Petersc363a232013-09-08 18:44:40 -05001138 if self._is_stopped or not self._started.is_set():
Antoine Pitrou7b476992013-09-07 23:38:37 +02001139 return False
Antoine Pitrou7b476992013-09-07 23:38:37 +02001140 self._wait_for_tstate_lock(False)
Tim Petersc363a232013-09-08 18:44:40 -05001141 return not self._is_stopped
Tim Petersb90f89a2001-01-15 03:26:36 +00001142
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001143 @property
1144 def daemon(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001145 """A boolean value indicating whether this thread is a daemon thread.
1146
1147 This must be set before start() is called, otherwise RuntimeError is
1148 raised. Its initial value is inherited from the creating thread; the
1149 main thread is not a daemon thread and therefore all threads created in
1150 the main thread default to daemon = False.
1151
mbarkhaubb110cc2019-06-22 14:51:06 +02001152 The entire Python program exits when only daemon threads are left.
Georg Brandlc30b59f2013-10-13 10:43:59 +02001153
1154 """
Guido van Rossumd0648992007-08-20 19:25:41 +00001155 assert self._initialized, "Thread.__init__() not called"
1156 return self._daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001157
Benjamin Petersonfdbea962008-08-18 17:33:47 +00001158 @daemon.setter
1159 def daemon(self, daemonic):
Guido van Rossumd0648992007-08-20 19:25:41 +00001160 if not self._initialized:
Guido van Rossumcd16bf62007-06-13 18:07:49 +00001161 raise RuntimeError("Thread.__init__() not called")
Benjamin Peterson672b8032008-06-11 19:14:14 +00001162 if self._started.is_set():
Antoine Pitrou10959072014-03-17 18:22:41 +01001163 raise RuntimeError("cannot set daemon status of active thread")
Guido van Rossumd0648992007-08-20 19:25:41 +00001164 self._daemonic = daemonic
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001165
Benjamin Peterson6640d722008-08-18 18:16:46 +00001166 def isDaemon(self):
Jelle Zijlstra9825bdf2021-04-12 01:42:53 -07001167 """Return whether this thread is a daemon.
1168
1169 This method is deprecated, use the daemon attribute instead.
1170
1171 """
1172 import warnings
1173 warnings.warn('isDaemon() is deprecated, get the daemon attribute instead',
1174 DeprecationWarning, stacklevel=2)
Benjamin Peterson6640d722008-08-18 18:16:46 +00001175 return self.daemon
1176
1177 def setDaemon(self, daemonic):
Jelle Zijlstra9825bdf2021-04-12 01:42:53 -07001178 """Set whether this thread is a daemon.
1179
1180 This method is deprecated, use the .daemon property instead.
1181
1182 """
1183 import warnings
1184 warnings.warn('setDaemon() is deprecated, set the daemon attribute instead',
1185 DeprecationWarning, stacklevel=2)
Benjamin Peterson6640d722008-08-18 18:16:46 +00001186 self.daemon = daemonic
1187
1188 def getName(self):
Jelle Zijlstra9825bdf2021-04-12 01:42:53 -07001189 """Return a string used for identification purposes only.
1190
1191 This method is deprecated, use the name attribute instead.
1192
1193 """
1194 import warnings
1195 warnings.warn('getName() is deprecated, get the name attribute instead',
1196 DeprecationWarning, stacklevel=2)
Benjamin Peterson6640d722008-08-18 18:16:46 +00001197 return self.name
1198
1199 def setName(self, name):
Jelle Zijlstra9825bdf2021-04-12 01:42:53 -07001200 """Set the name string for this thread.
1201
1202 This method is deprecated, use the name attribute instead.
1203
1204 """
1205 import warnings
1206 warnings.warn('setName() is deprecated, set the name attribute instead',
1207 DeprecationWarning, stacklevel=2)
Benjamin Peterson6640d722008-08-18 18:16:46 +00001208 self.name = name
1209
Victor Stinnercd590a72019-05-28 00:39:52 +02001210
1211try:
1212 from _thread import (_excepthook as excepthook,
1213 _ExceptHookArgs as ExceptHookArgs)
1214except ImportError:
1215 # Simple Python implementation if _thread._excepthook() is not available
1216 from traceback import print_exception as _print_exception
1217 from collections import namedtuple
1218
1219 _ExceptHookArgs = namedtuple(
1220 'ExceptHookArgs',
1221 'exc_type exc_value exc_traceback thread')
1222
1223 def ExceptHookArgs(args):
1224 return _ExceptHookArgs(*args)
1225
1226 def excepthook(args, /):
1227 """
1228 Handle uncaught Thread.run() exception.
1229 """
1230 if args.exc_type == SystemExit:
1231 # silently ignore SystemExit
1232 return
1233
1234 if _sys is not None and _sys.stderr is not None:
1235 stderr = _sys.stderr
1236 elif args.thread is not None:
1237 stderr = args.thread._stderr
1238 if stderr is None:
1239 # do nothing if sys.stderr is None and sys.stderr was None
1240 # when the thread was created
1241 return
1242 else:
1243 # do nothing if sys.stderr is None and args.thread is None
1244 return
1245
1246 if args.thread is not None:
1247 name = args.thread.name
1248 else:
1249 name = get_ident()
1250 print(f"Exception in thread {name}:",
1251 file=stderr, flush=True)
1252 _print_exception(args.exc_type, args.exc_value, args.exc_traceback,
1253 file=stderr)
1254 stderr.flush()
1255
1256
Mario Corchero750c5ab2020-11-12 18:27:44 +01001257# Original value of threading.excepthook
1258__excepthook__ = excepthook
1259
1260
Victor Stinnercd590a72019-05-28 00:39:52 +02001261def _make_invoke_excepthook():
1262 # Create a local namespace to ensure that variables remain alive
1263 # when _invoke_excepthook() is called, even if it is called late during
1264 # Python shutdown. It is mostly needed for daemon threads.
1265
1266 old_excepthook = excepthook
1267 old_sys_excepthook = _sys.excepthook
1268 if old_excepthook is None:
1269 raise RuntimeError("threading.excepthook is None")
1270 if old_sys_excepthook is None:
1271 raise RuntimeError("sys.excepthook is None")
1272
1273 sys_exc_info = _sys.exc_info
1274 local_print = print
1275 local_sys = _sys
1276
1277 def invoke_excepthook(thread):
1278 global excepthook
1279 try:
1280 hook = excepthook
1281 if hook is None:
1282 hook = old_excepthook
1283
1284 args = ExceptHookArgs([*sys_exc_info(), thread])
1285
1286 hook(args)
1287 except Exception as exc:
1288 exc.__suppress_context__ = True
1289 del exc
1290
1291 if local_sys is not None and local_sys.stderr is not None:
1292 stderr = local_sys.stderr
1293 else:
1294 stderr = thread._stderr
1295
1296 local_print("Exception in threading.excepthook:",
1297 file=stderr, flush=True)
1298
1299 if local_sys is not None and local_sys.excepthook is not None:
1300 sys_excepthook = local_sys.excepthook
1301 else:
1302 sys_excepthook = old_sys_excepthook
1303
1304 sys_excepthook(*sys_exc_info())
1305 finally:
1306 # Break reference cycle (exception stored in a variable)
1307 args = None
1308
1309 return invoke_excepthook
1310
1311
Martin v. Löwis44f86962001-09-05 13:44:54 +00001312# The timer class was contributed by Itamar Shtull-Trauring
1313
Éric Araujo0cdd4452011-07-28 00:28:28 +02001314class Timer(Thread):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001315 """Call a function after a specified number of seconds:
Tim Petersb64bec32001-09-18 02:26:39 +00001316
Georg Brandlc30b59f2013-10-13 10:43:59 +02001317 t = Timer(30.0, f, args=None, kwargs=None)
1318 t.start()
1319 t.cancel() # stop the timer's action if it's still waiting
1320
Martin v. Löwis44f86962001-09-05 13:44:54 +00001321 """
Tim Petersb64bec32001-09-18 02:26:39 +00001322
R David Murray19aeb432013-03-30 17:19:38 -04001323 def __init__(self, interval, function, args=None, kwargs=None):
Martin v. Löwis44f86962001-09-05 13:44:54 +00001324 Thread.__init__(self)
1325 self.interval = interval
1326 self.function = function
R David Murray19aeb432013-03-30 17:19:38 -04001327 self.args = args if args is not None else []
1328 self.kwargs = kwargs if kwargs is not None else {}
Martin v. Löwis44f86962001-09-05 13:44:54 +00001329 self.finished = Event()
Tim Petersb64bec32001-09-18 02:26:39 +00001330
Martin v. Löwis44f86962001-09-05 13:44:54 +00001331 def cancel(self):
Georg Brandlc30b59f2013-10-13 10:43:59 +02001332 """Stop the timer if it hasn't finished yet."""
Martin v. Löwis44f86962001-09-05 13:44:54 +00001333 self.finished.set()
Tim Petersb64bec32001-09-18 02:26:39 +00001334
Martin v. Löwis44f86962001-09-05 13:44:54 +00001335 def run(self):
1336 self.finished.wait(self.interval)
Benjamin Peterson672b8032008-06-11 19:14:14 +00001337 if not self.finished.is_set():
Martin v. Löwis44f86962001-09-05 13:44:54 +00001338 self.function(*self.args, **self.kwargs)
1339 self.finished.set()
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001340
Antoine Pitrou1023dbb2017-10-02 16:42:15 +02001341
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001342# Special thread class to represent the main thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001343
1344class _MainThread(Thread):
1345
1346 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001347 Thread.__init__(self, name="MainThread", daemon=False)
Tim Petersc363a232013-09-08 18:44:40 -05001348 self._set_tstate_lock()
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001349 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001350 self._set_ident()
Jake Teslerb121f632019-05-22 08:43:17 -07001351 if _HAVE_THREAD_NATIVE_ID:
1352 self._set_native_id()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001353 with _active_limbo_lock:
1354 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001355
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001356
1357# Dummy thread class to represent threads not started here.
Tim Peters711906e2005-01-08 07:30:42 +00001358# These aren't garbage collected when they die, nor can they be waited for.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001359# If they invoke anything in threading.py that calls current_thread(), they
Tim Peters711906e2005-01-08 07:30:42 +00001360# leave an entry in the _active dict forever after.
Benjamin Peterson672b8032008-06-11 19:14:14 +00001361# Their purpose is to return *something* from current_thread().
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001362# They are marked as daemon threads so we won't wait for them
1363# when we exit (conform previous semantics).
1364
1365class _DummyThread(Thread):
Tim Petersb90f89a2001-01-15 03:26:36 +00001366
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001367 def __init__(self):
Antoine Pitrou0bd4deb2011-02-25 22:07:43 +00001368 Thread.__init__(self, name=_newname("Dummy-%d"), daemon=True)
Tim Peters711906e2005-01-08 07:30:42 +00001369
Christian Heimes9e7f1d22008-02-28 12:27:11 +00001370 self._started.set()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001371 self._set_ident()
Jake Teslerb121f632019-05-22 08:43:17 -07001372 if _HAVE_THREAD_NATIVE_ID:
1373 self._set_native_id()
Benjamin Petersond23f8222009-04-05 19:13:16 +00001374 with _active_limbo_lock:
1375 _active[self._ident] = self
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001376
Antoine Pitrou8e6e0fd2012-04-19 23:55:01 +02001377 def _stop(self):
1378 pass
1379
Xiang Zhangf3a9fab2017-02-27 11:01:30 +08001380 def is_alive(self):
1381 assert not self._is_stopped and self._started.is_set()
1382 return True
1383
Neal Norwitz45bec8c2002-02-19 03:01:36 +00001384 def join(self, timeout=None):
Guido van Rossum8ca162f2002-04-07 06:36:23 +00001385 assert False, "cannot join a dummy thread"
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001386
1387
1388# Global API functions
1389
Benjamin Peterson672b8032008-06-11 19:14:14 +00001390def current_thread():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001391 """Return the current Thread object, corresponding to the caller's thread of control.
1392
1393 If the caller's thread of control was not created through the threading
1394 module, a dummy thread object with limited functionality is returned.
1395
1396 """
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001397 try:
Victor Stinner2a129742011-05-30 23:02:52 +02001398 return _active[get_ident()]
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001399 except KeyError:
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001400 return _DummyThread()
1401
Jelle Zijlstra9825bdf2021-04-12 01:42:53 -07001402def currentThread():
1403 """Return the current Thread object, corresponding to the caller's thread of control.
1404
1405 This function is deprecated, use current_thread() instead.
1406
1407 """
1408 import warnings
1409 warnings.warn('currentThread() is deprecated, use current_thread() instead',
1410 DeprecationWarning, stacklevel=2)
1411 return current_thread()
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001412
Benjamin Peterson672b8032008-06-11 19:14:14 +00001413def active_count():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001414 """Return the number of Thread objects currently alive.
1415
1416 The returned count is equal to the length of the list returned by
1417 enumerate().
1418
1419 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001420 with _active_limbo_lock:
1421 return len(_active) + len(_limbo)
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001422
Jelle Zijlstra9825bdf2021-04-12 01:42:53 -07001423def activeCount():
1424 """Return the number of Thread objects currently alive.
1425
1426 This function is deprecated, use active_count() instead.
1427
1428 """
1429 import warnings
1430 warnings.warn('activeCount() is deprecated, use active_count() instead',
1431 DeprecationWarning, stacklevel=2)
1432 return active_count()
Benjamin Petersonf0923f52008-08-18 22:10:13 +00001433
Antoine Pitroubdec11f2009-11-05 13:49:14 +00001434def _enumerate():
1435 # Same as enumerate(), but without the lock. Internal use only.
1436 return list(_active.values()) + list(_limbo.values())
1437
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001438def enumerate():
Georg Brandlc30b59f2013-10-13 10:43:59 +02001439 """Return a list of all Thread objects currently alive.
1440
1441 The list includes daemonic threads, dummy thread objects created by
1442 current_thread(), and the main thread. It excludes terminated threads and
1443 threads that have not yet been started.
1444
1445 """
Benjamin Petersond23f8222009-04-05 19:13:16 +00001446 with _active_limbo_lock:
1447 return list(_active.values()) + list(_limbo.values())
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001448
Kyle Stanleyb61b8182020-03-27 15:31:22 -04001449
1450_threading_atexits = []
1451_SHUTTING_DOWN = False
1452
1453def _register_atexit(func, *arg, **kwargs):
1454 """CPython internal: register *func* to be called before joining threads.
1455
1456 The registered *func* is called with its arguments just before all
1457 non-daemon threads are joined in `_shutdown()`. It provides a similar
1458 purpose to `atexit.register()`, but its functions are called prior to
1459 threading shutdown instead of interpreter shutdown.
1460
1461 For similarity to atexit, the registered functions are called in reverse.
1462 """
1463 if _SHUTTING_DOWN:
1464 raise RuntimeError("can't register atexit after shutdown")
1465
1466 call = functools.partial(func, *arg, **kwargs)
1467 _threading_atexits.append(call)
1468
1469
Georg Brandl2067bfd2008-05-25 13:05:15 +00001470from _thread import stack_size
Thomas Wouters0e3f5912006-08-11 14:57:12 +00001471
Thomas Wouters902d6eb2007-01-09 23:18:33 +00001472# Create the main thread object,
1473# and make it available for the interpreter
1474# (Py_Main) as threading._shutdown.
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001475
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001476_main_thread = _MainThread()
1477
1478def _shutdown():
Victor Stinner468e5fe2019-06-13 01:30:17 +02001479 """
1480 Wait until the Python thread state of all non-daemon threads get deleted.
1481 """
Tim Petersc363a232013-09-08 18:44:40 -05001482 # Obscure: other threads may be waiting to join _main_thread. That's
1483 # dubious, but some code does it. We can't wait for C code to release
1484 # the main thread's tstate_lock - that won't happen until the interpreter
1485 # is nearly dead. So we release it here. Note that just calling _stop()
1486 # isn't enough: other threads may already be waiting on _tstate_lock.
Antoine Pitrouee84a602017-08-16 20:53:28 +02001487 if _main_thread._is_stopped:
1488 # _shutdown() was already called
1489 return
Victor Stinner468e5fe2019-06-13 01:30:17 +02001490
Kyle Stanleyb61b8182020-03-27 15:31:22 -04001491 global _SHUTTING_DOWN
1492 _SHUTTING_DOWN = True
Victor Stinner468e5fe2019-06-13 01:30:17 +02001493 # Main thread
Tim Petersb5e9ac92013-09-09 14:41:50 -05001494 tlock = _main_thread._tstate_lock
1495 # The main thread isn't finished yet, so its thread state lock can't have
1496 # been released.
1497 assert tlock is not None
1498 assert tlock.locked()
1499 tlock.release()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001500 _main_thread._stop()
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001501
Kyle Stanleyb61b8182020-03-27 15:31:22 -04001502 # Call registered threading atexit functions before threads are joined.
1503 # Order is reversed, similar to atexit.
1504 for atexit_call in reversed(_threading_atexits):
1505 atexit_call()
1506
Victor Stinner468e5fe2019-06-13 01:30:17 +02001507 # Join all non-deamon threads
1508 while True:
1509 with _shutdown_locks_lock:
1510 locks = list(_shutdown_locks)
1511 _shutdown_locks.clear()
1512
1513 if not locks:
1514 break
1515
1516 for lock in locks:
1517 # mimick Thread.join()
1518 lock.acquire()
1519 lock.release()
1520
1521 # new threads can be spawned while we were waiting for the other
1522 # threads to complete
1523
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001524
1525def main_thread():
Andrew Svetlovb1dd5572013-09-04 10:33:11 +03001526 """Return the main thread object.
1527
1528 In normal conditions, the main thread is the thread from which the
1529 Python interpreter was started.
1530 """
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001531 return _main_thread
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001532
Jim Fultond15dc062004-07-14 19:11:50 +00001533# get thread-local implementation, either from the thread
1534# module, or from the python fallback
1535
1536try:
Georg Brandl2067bfd2008-05-25 13:05:15 +00001537 from _thread import _local as local
Brett Cannoncd171c82013-07-04 17:43:24 -04001538except ImportError:
Jim Fultond15dc062004-07-14 19:11:50 +00001539 from _threading_local import local
1540
Guido van Rossum7f5013a1998-04-09 22:01:42 +00001541
Jesse Nollera8513972008-07-17 16:49:17 +00001542def _after_fork():
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02001543 """
1544 Cleanup threading module state that should not exist after a fork.
1545 """
Jesse Nollera8513972008-07-17 16:49:17 +00001546 # Reset _active_limbo_lock, in case we forked while the lock was held
1547 # by another (non-forked) thread. http://bugs.python.org/issue874900
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001548 global _active_limbo_lock, _main_thread
Victor Stinner468e5fe2019-06-13 01:30:17 +02001549 global _shutdown_locks_lock, _shutdown_locks
Jesse Nollera8513972008-07-17 16:49:17 +00001550 _active_limbo_lock = _allocate_lock()
1551
1552 # fork() only copied the current thread; clear references to others.
1553 new_active = {}
Victor Stinnerd8ff44c2020-03-27 17:50:42 +01001554
1555 try:
1556 current = _active[get_ident()]
1557 except KeyError:
1558 # fork() was called in a thread which was not spawned
1559 # by threading.Thread. For example, a thread spawned
1560 # by thread.start_new_thread().
1561 current = _MainThread()
1562
Andrew Svetlov58b5c5a2013-09-04 07:01:07 +03001563 _main_thread = current
Victor Stinner468e5fe2019-06-13 01:30:17 +02001564
1565 # reset _shutdown() locks: threads re-register their _tstate_lock below
1566 _shutdown_locks_lock = _allocate_lock()
1567 _shutdown_locks = set()
1568
Jesse Nollera8513972008-07-17 16:49:17 +00001569 with _active_limbo_lock:
Antoine Pitrou5da7e792013-09-08 13:19:06 +02001570 # Dangling thread instances must still have their locks reset,
1571 # because someone may join() them.
1572 threads = set(_enumerate())
1573 threads.update(_dangling)
1574 for thread in threads:
Charles-François Natalib055bf62011-12-18 18:45:16 +01001575 # Any lock/condition variable may be currently locked or in an
1576 # invalid state, so we reinitialize them.
Jesse Nollera8513972008-07-17 16:49:17 +00001577 if thread is current:
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001578 # There is only one active thread. We reset the ident to
1579 # its new value since it can have changed.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001580 thread._reset_internal_locks(True)
Victor Stinner2a129742011-05-30 23:02:52 +02001581 ident = get_ident()
Antoine Pitrou5fe291f2008-09-06 23:00:03 +00001582 thread._ident = ident
Jesse Nollera8513972008-07-17 16:49:17 +00001583 new_active[ident] = thread
1584 else:
1585 # All the others are already stopped.
Antoine Pitrou7b476992013-09-07 23:38:37 +02001586 thread._reset_internal_locks(False)
Charles-François Natalib055bf62011-12-18 18:45:16 +01001587 thread._stop()
Jesse Nollera8513972008-07-17 16:49:17 +00001588
1589 _limbo.clear()
1590 _active.clear()
1591 _active.update(new_active)
1592 assert len(_active) == 1
Antoine Pitrou4a8bcdf2017-05-28 14:02:26 +02001593
1594
Gregory P. Smith163468a2017-05-29 10:03:41 -07001595if hasattr(_os, "register_at_fork"):
1596 _os.register_at_fork(after_in_child=_after_fork)