1
"""Thread module emulating a subset of Java's threading model."""
8
from time import monotonic as _time
9
from _weakrefset import WeakSet
10
from itertools import count as _count
12
from _collections import deque as _deque
14
from collections import deque as _deque
16
# 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
19
# language. Those original names are not in any imminent danger of
20
# 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.
26
__all__ = ['get_ident', 'active_count', 'Condition', 'current_thread',
27
'enumerate', 'main_thread', 'TIMEOUT_MAX',
28
'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
29
'Barrier', 'BrokenBarrierError', 'Timer', 'ThreadError',
30
'setprofile', 'settrace', 'local', 'stack_size',
31
'excepthook', 'ExceptHookArgs', 'gettrace', 'getprofile',
32
'serialize_iterator', 'synchronized_iterator', 'concurrent_tee',
33
'setprofile_all_threads','settrace_all_threads']
35
# Rename some stuff so "from threading import *" is safe
36
_start_joinable_thread = _thread.start_joinable_thread
37
_daemon_threads_allowed = _thread.daemon_threads_allowed
38
_allocate_lock = _thread.allocate_lock
39
_LockType = _thread.LockType
40
_thread_shutdown = _thread._shutdown
41
_make_thread_handle = _thread._make_thread_handle
42
_ThreadHandle = _thread._ThreadHandle
43
get_ident = _thread.get_ident
44
_get_main_thread_ident = _thread._get_main_thread_ident
45
_is_main_interpreter = _thread._is_main_interpreter
47
get_native_id = _thread.get_native_id
48
_HAVE_THREAD_NATIVE_ID = True
49
__all__.append('get_native_id')
50
except AttributeError:
51
_HAVE_THREAD_NATIVE_ID = False
53
_set_name = _thread.set_name
54
except AttributeError:
56
ThreadError = _thread.error
58
_CRLock = _thread.RLock
59
except AttributeError:
61
TIMEOUT_MAX = _thread.TIMEOUT_MAX
64
# get thread-local implementation, either from the thread
65
# module, or from the python fallback
68
from _thread import _local as local
70
from _threading_local import local
72
# Support for profile and trace hooks
78
"""Set a profile function for all threads started from the threading module.
80
The func will be passed to sys.setprofile() for each thread, before its
81
run() method is called.
86
def setprofile_all_threads(func):
87
"""Set a profile function for all threads started from the threading module
88
and all Python threads that are currently executing.
90
The func will be passed to sys.setprofile() for each thread, before its
91
run() method is called.
94
_sys._setprofileallthreads(func)
97
"""Get the profiler function as set by threading.setprofile()."""
101
"""Set a trace function for all threads started from the threading module.
103
The func will be passed to sys.settrace() for each thread, before its run()
109
def settrace_all_threads(func):
110
"""Set a trace function for all threads started from the threading module
111
and all Python threads that are currently executing.
113
The func will be passed to sys.settrace() for each thread, before its run()
117
_sys._settraceallthreads(func)
120
"""Get the trace function as set by threading.settrace()."""
123
# Synchronization classes
128
"""Factory function that returns a new reentrant lock.
130
A reentrant lock must be released by the thread that acquired it. Once a
131
thread has acquired a reentrant lock, the same thread may acquire it again
132
without blocking; the thread must release it once for each time it has
141
"""This class implements reentrant lock objects.
143
A reentrant lock must be released by the thread that acquired it. Once a
144
thread has acquired a reentrant lock, the same thread may acquire it
145
again without blocking; the thread must release it once for each time it
151
self._block = _allocate_lock()
158
owner = _active[owner].name
161
return "<%s %s.%s object owner=%r count=%d at %s>" % (
162
"locked" if self.locked() else "unlocked",
163
self.__class__.__module__,
164
self.__class__.__qualname__,
170
def _at_fork_reinit(self):
171
self._block._at_fork_reinit()
175
def acquire(self, blocking=True, timeout=-1):
176
"""Acquire a lock, blocking or non-blocking.
178
When invoked without arguments: if this thread already owns the lock,
179
increment the recursion level by one, and return immediately. Otherwise,
180
if another thread owns the lock, block until the lock is unlocked. Once
181
the lock is unlocked (not owned by any thread), then grab ownership, set
182
the recursion level to one, and return. If more than one thread is
183
blocked waiting until the lock is unlocked, only one at a time will be
184
able to grab ownership of the lock. There is no return value in this
187
When invoked with the blocking argument set to true, do the same thing
188
as when called without arguments, and return true.
190
When invoked with the blocking argument set to false, do not block. If a
191
call without an argument would block, return false immediately;
192
otherwise, do the same thing as when called without arguments, and
195
When invoked with the floating-point timeout argument set to a positive
196
value, block for at most the number of seconds specified by timeout
197
and as long as the lock cannot be acquired. Return true if the lock has
198
been acquired, false if the timeout has elapsed.
202
if self._owner == me:
205
rc = self._block.acquire(blocking, timeout)
214
"""Release a lock, decrementing the recursion level.
216
If after the decrement it is zero, reset the lock to unlocked (not owned
217
by any thread), and if any other threads are blocked waiting for the
218
lock to become unlocked, allow exactly one of them to proceed. If after
219
the decrement the recursion level is still nonzero, the lock remains
220
locked and owned by the calling thread.
222
Only call this method when the calling thread owns the lock. A
223
RuntimeError is raised if this method is called when the lock is
226
There is no return value.
229
if self._owner != get_ident():
230
raise RuntimeError("cannot release un-acquired lock")
231
self._count = count = self._count - 1
234
self._block.release()
236
def __exit__(self, t, v, tb):
240
"""Return whether this object is locked."""
241
return self._block.locked()
243
# Internal methods used by condition variables
245
def _acquire_restore(self, state):
246
self._block.acquire()
247
self._count, self._owner = state
249
def _release_save(self):
251
raise RuntimeError("cannot release un-acquired lock")
256
self._block.release()
257
return (count, owner)
260
return self._owner == get_ident()
262
# Internal method used for reentrancy checks
264
def _recursion_count(self):
265
if self._owner != get_ident():
273
"""Class that implements a condition variable.
275
A condition variable allows one or more threads to wait until they are
276
notified by another thread.
278
If the lock argument is given and not None, it must be a Lock or RLock
279
object, and it is used as the underlying lock. Otherwise, a new RLock object
280
is created and used as the underlying lock.
284
def __init__(self, lock=None):
288
# Export the lock's acquire(), release(), and locked() methods
289
self.acquire = lock.acquire
290
self.release = lock.release
291
self.locked = lock.locked
292
# If the lock defines _release_save() and/or _acquire_restore(),
293
# these override the default implementations (which just call
294
# release() and acquire() on the lock). Ditto for _is_owned().
295
if hasattr(lock, '_release_save'):
296
self._release_save = lock._release_save
297
if hasattr(lock, '_acquire_restore'):
298
self._acquire_restore = lock._acquire_restore
299
if hasattr(lock, '_is_owned'):
300
self._is_owned = lock._is_owned
301
self._waiters = _deque()
303
def _at_fork_reinit(self):
304
self._lock._at_fork_reinit()
305
self._waiters.clear()
308
return self._lock.__enter__()
310
def __exit__(self, *args):
311
1
1
return self._lock.__exit__(*args)
314
return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
316
def _release_save(self):
317
self._lock.release() # No state to save
319
def _acquire_restore(self, x):
320
self._lock.acquire() # Ignore saved state
323
# Return True if lock is owned by current_thread.
324
# This method is called only if _lock doesn't have _is_owned().
325
if self._lock.acquire(False):
331
def wait(self, timeout=None):
332
"""Wait until notified or until a timeout occurs.
334
If the calling thread has not acquired the lock when this method is
335
called, a RuntimeError is raised.
337
This method releases the underlying lock, and then blocks until it is
338
awakened by a notify() or notify_all() call for the same condition
339
variable in another thread, or until the optional timeout occurs. Once
340
awakened or timed out, it re-acquires the lock and returns.
342
When the timeout argument is present and not None, it should be a
343
floating-point number specifying a timeout for the operation in seconds
344
(or fractions thereof).
346
When the underlying lock is an RLock, it is not released using its
347
release() method, since this may not actually unlock the lock when it
348
was acquired multiple times recursively. Instead, an internal interface
349
of the RLock class is used, which really unlocks it even when it has
350
been recursively acquired several times. Another internal interface is
351
then used to restore the recursion level when the lock is reacquired.
354
if not self._is_owned():
355
raise RuntimeError("cannot wait on un-acquired lock")
356
waiter = _allocate_lock()
358
1
1
self._waiters.append(waiter)
359
saved_state = self._release_save()
361
try: # restore state no matter what (e.g., KeyboardInterrupt)
363
12
12
waiter.acquire()
367
gotit = waiter.acquire(True, timeout)
369
gotit = waiter.acquire(False)
372
1
1
self._acquire_restore(saved_state)
375
self._waiters.remove(waiter)
379
def wait_for(self, predicate, timeout=None):
380
"""Wait until a condition evaluates to True.
382
predicate should be a callable which result will be interpreted as a
383
boolean value. A timeout may be provided giving the maximum time to
391
if waittime is not None:
393
endtime = _time() + waittime
395
waittime = endtime - _time()
402
def notify(self, n=1):
403
"""Wake up one or more threads waiting on this condition, if any.
405
If the calling thread has not acquired the lock when this method is
406
called, a RuntimeError is raised.
408
This method wakes up at most n of the threads waiting for the condition
409
variable; it is a no-op if no threads are waiting.
412
if not self._is_owned():
413
raise RuntimeError("cannot notify on un-acquired lock")
414
waiters = self._waiters
415
while waiters and n > 0:
420
# gh-92530: The previous call of notify() released the lock,
421
# but was interrupted before removing it from the queue.
422
# It can happen if a signal handler raises an exception,
423
# like CTRL+C which raises KeyboardInterrupt.
428
waiters.remove(waiter)
432
def notify_all(self):
433
"""Wake up all threads waiting on this condition.
435
If the calling thread has not acquired the lock when this method
436
is called, a RuntimeError is raised.
439
self.notify(len(self._waiters))
442
"""Wake up all threads waiting on this condition.
444
This method is deprecated, use notify_all() instead.
448
warnings.warn('notifyAll() is deprecated, use notify_all() instead',
449
DeprecationWarning, stacklevel=2)
454
"""This class implements semaphore objects.
456
Semaphores manage a counter representing the number of release() calls minus
457
the number of acquire() calls, plus an initial value. The acquire() method
458
blocks if necessary until it can return without making the counter
459
negative. If not given, value defaults to 1.
463
# After Tim Peters' semaphore class, but not quite the same (no maximum)
465
def __init__(self, value=1):
467
raise ValueError("semaphore initial value must be >= 0")
468
self._cond = Condition(Lock())
473
return (f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}:"
474
f" value={self._value}>")
476
def acquire(self, blocking=True, timeout=None):
477
"""Acquire a semaphore, decrementing the internal counter by one.
479
When invoked without arguments: if the internal counter is larger than
480
zero on entry, decrement it by one and return immediately. If it is zero
481
on entry, block, waiting until some other thread has called release() to
482
make it larger than zero. This is done with proper interlocking so that
483
if multiple acquire() calls are blocked, release() will wake exactly one
484
of them up. The implementation may pick one at random, so the order in
485
which blocked threads are awakened should not be relied on. There is no
486
return value in this case.
488
When invoked with blocking set to true, do the same thing as when called
489
without arguments, and return true.
491
When invoked with blocking set to false, do not block. If a call without
492
an argument would block, return false immediately; otherwise, do the
493
same thing as when called without arguments, and return true.
495
When invoked with a timeout other than None, it will block for at
496
most timeout seconds. If acquire does not complete successfully in
497
that interval, return false. Return true otherwise.
500
if not blocking and timeout is not None:
501
raise ValueError("can't specify timeout for non-blocking acquire")
505
while self._value == 0:
508
if timeout is not None:
510
endtime = _time() + timeout
512
timeout = endtime - _time()
515
self._cond.wait(timeout)
523
def release(self, n=1):
524
"""Release a semaphore, incrementing the internal counter by one or more.
526
When the counter is zero on entry and another thread is waiting for it
527
to become larger than zero again, wake up that thread.
531
raise ValueError('n must be one or more')
536
def __exit__(self, t, v, tb):
540
class BoundedSemaphore(Semaphore):
541
"""Implements a bounded semaphore.
543
A bounded semaphore checks to make sure its current value doesn't exceed its
544
initial value. If it does, ValueError is raised. In most situations
545
semaphores are used to guard resources with limited capacity.
547
If the semaphore is released too many times it's a sign of a bug. If not
548
given, value defaults to 1.
550
Like regular semaphores, bounded semaphores manage a counter representing
551
the number of release() calls minus the number of acquire() calls, plus an
552
initial value. The acquire() method blocks if necessary until it can return
553
without making the counter negative. If not given, value defaults to 1.
557
def __init__(self, value=1):
558
super().__init__(value)
559
self._initial_value = value
563
return (f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}:"
564
f" value={self._value}/{self._initial_value}>")
566
def release(self, n=1):
567
"""Release a semaphore, incrementing the internal counter by one or more.
569
When the counter is zero on entry and another thread is waiting for it
570
to become larger than zero again, wake up that thread.
572
If the number of releases exceeds the number of acquires,
577
raise ValueError('n must be one or more')
579
if self._value + n > self._initial_value:
580
raise ValueError("Semaphore released too many times")
586
"""Class implementing event objects.
588
Events manage a flag that can be set to true with the set() method and reset
589
to false with the clear() method. The wait() method blocks until the flag is
590
true. The flag is initially false.
594
# After Tim Peters' event class (without is_posted())
597
self._cond = Condition(Lock())
602
status = 'set' if self._flag else 'unset'
603
return f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}: {status}>"
605
def _at_fork_reinit(self):
606
# Private method called by Thread._after_fork()
607
self._cond._at_fork_reinit()
610
"""Return true if and only if the internal flag is true."""
614
"""Return true if and only if the internal flag is true.
616
This method is deprecated, use is_set() instead.
620
warnings.warn('isSet() is deprecated, use is_set() instead',
621
DeprecationWarning, stacklevel=2)
625
"""Set the internal flag to true.
627
All threads waiting for it to become true are awakened. Threads
628
that call wait() once the flag is true will not block at all.
633
self._cond.notify_all()
636
"""Reset the internal flag to false.
638
Subsequently, threads calling wait() will block until set() is called to
639
set the internal flag to true again.
645
def wait(self, timeout=None):
646
"""Block until the internal flag is true.
648
If the internal flag is true on entry, return immediately. Otherwise,
649
block until another thread calls set() to set the flag to true, or until
650
the optional timeout occurs.
652
When the timeout argument is present and not None, it should be a
653
floating-point number specifying a timeout for the operation in seconds
654
(or fractions thereof).
656
This method returns the internal flag on exit, so it will always return
657
``True`` except if a timeout is given and the operation times out, when
658
it will return ``False``.
662
signaled = self._flag
664
signaled = self._cond.wait(timeout)
668
# A barrier class. Inspired in part by the pthread_barrier_* api and
669
# the CyclicBarrier class from Java. See
670
# http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
671
# http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
674
# We maintain two main states, 'filling' and 'draining' enabling the barrier
675
# to be cyclic. Threads are not allowed into it until it has fully drained
676
# since the previous cycle. In addition, a 'resetting' state exists which is
677
# similar to 'draining' except that threads leave with a BrokenBarrierError,
678
# and a 'broken' state in which all threads get the exception.
680
"""Implements a Barrier.
682
Useful for synchronizing a fixed number of threads at known synchronization
683
points. Threads block on 'wait()' and are simultaneously awoken once they
684
have all made that call.
688
def __init__(self, parties, action=None, timeout=None):
689
"""Create a barrier, initialised to 'parties' threads.
691
'action' is a callable which, when supplied, will be called by one of
692
the threads after they have all entered the barrier and just prior to
693
releasing them all. If a 'timeout' is provided, it is used as the
694
default for all subsequent 'wait()' calls.
698
raise ValueError("parties must be >= 1")
699
self._cond = Condition(Lock())
700
self._action = action
701
self._timeout = timeout
702
self._parties = parties
703
self._state = 0 # 0 filling, 1 draining, -1 resetting, -2 broken
709
return f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}: broken>"
710
return (f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}:"
711
f" waiters={self.n_waiting}/{self.parties}>")
713
def wait(self, timeout=None):
714
"""Wait for the barrier.
716
When the specified number of threads have started waiting, they are all
717
simultaneously awoken. If an 'action' was provided for the barrier, one
718
of the threads will have executed that callback prior to returning.
719
Returns an individual index number from 0 to 'parties-1'.
723
timeout = self._timeout
725
self._enter() # Block while the barrier drains.
729
if index + 1 == self._parties:
730
# We release the barrier
733
# We wait until someone releases us
738
# Wake up any threads waiting for barrier to drain.
741
# Block until the barrier is ready for us, or raise an exception
744
while self._state in (-1, 1):
745
# It is draining or resetting, wait until done
747
#see if the barrier is in a broken state
749
raise BrokenBarrierError
750
assert self._state == 0
752
# Optionally run the 'action' and release the threads waiting
758
# enter draining state
760
self._cond.notify_all()
762
#an exception during the _action handler. Break and reraise
766
# Wait in the barrier until we are released. Raise an exception
767
# if the barrier is reset or broken.
768
def _wait(self, timeout):
769
if not self._cond.wait_for(lambda : self._state != 0, timeout):
770
#timed out. Break the barrier
772
raise BrokenBarrierError
774
raise BrokenBarrierError
775
assert self._state == 1
777
# If we are the last thread to exit the barrier, signal any threads
778
# waiting for the barrier to drain.
781
if self._state in (-1, 1):
782
#resetting or draining
784
self._cond.notify_all()
787
"""Reset the barrier to the initial state.
789
Any threads currently waiting will get the BrokenBarrier exception
796
#reset the barrier, waking up threads
798
elif self._state == -2:
799
#was broken, set it to reset state
800
#which clears when the last thread exits
804
self._cond.notify_all()
807
"""Place the barrier into a 'broken' state.
809
Useful in case of error. Any currently waiting threads and threads
810
attempting to 'wait()' will have BrokenBarrierError raised.
817
# An internal error was detected. The barrier is set to
818
# a broken state all parties awakened.
820
self._cond.notify_all()
824
"""Return the number of threads required to trip the barrier."""
829
"""Return the number of threads currently waiting at the barrier."""
830
# We don't need synchronization here since this is an ephemeral result
831
# anyway. It returns the correct value in the steady state.
838
"""Return True if the barrier is in a broken state."""
839
return self._state == -2
841
# exception raised by the Barrier class
842
class BrokenBarrierError(RuntimeError):
846
## Synchronization tools for iterators #####################
848
class serialize_iterator:
849
"""Wrap a non-concurrent iterator with a lock to enforce sequential access.
851
Applies a non-reentrant lock around calls to __next__. If the
852
wrapped iterator also defines send(), throw(), or close(), those
853
calls are serialized as well.
855
Allows iterator and generator instances to be shared by multiple consumer
858
For example, itertools.count does not make thread-safe instances,
859
but that is easily fixed with:
861
atomic_counter = serialize_iterator(itertools.count())
865
__slots__ = ('_iterator', '_lock')
867
def __init__(self, iterable):
868
self._iterator = iter(iterable)
876
return next(self._iterator)
878
def send(self, value, /):
879
"""Send a value to a generator.
881
Raises AttributeError if not a generator.
884
return self._iterator.send(value)
886
def throw(self, *args):
887
"""Call throw() on a generator.
889
Raises AttributeError if not a generator.
892
return self._iterator.throw(*args)
895
"""Call close() on a generator.
897
Raises AttributeError if not a generator.
900
return self._iterator.close()
903
def synchronized_iterator(func):
904
"""Wrap an iterator-returning callable to make its iterators thread-safe.
906
Existing itertools and more-itertools can be wrapped so that their
907
iterator instances are serialized.
909
For example, itertools.count does not make thread-safe instances,
910
but that is easily fixed with:
912
atomic_counter = synchronized_iterator(itertools.count)
914
Can also be used as a decorator for generator function definitions
915
so that the generator instances are serialized::
919
@synchronized_iterator
920
def enumerate_and_timestamp(iterable):
921
for count, value in enumerate(iterable):
922
yield count, time.time_ns(), value
926
from functools import wraps
929
def inner(*args, **kwargs):
930
iterator = func(*args, **kwargs)
931
return serialize_iterator(iterator)
936
def concurrent_tee(iterable, n=2):
937
"""Variant of itertools.tee() but with guaranteed threading semantics.
939
Takes a non-threadsafe iterator as an input and creates concurrent
940
tee objects for other threads to have reliable independent copies of
943
The new iterators are only thread-safe if consumed within a single thread.
944
To share just one of the new iterators across multiple threads, wrap it
945
with threading.serialize_iterator().
949
raise ValueError("n must be a non-negative integer")
952
iterator = _concurrent_tee(iterable)
954
for _ in range(n - 1):
955
result.append(_concurrent_tee(iterator))
959
class _concurrent_tee:
960
__slots__ = ('iterator', 'link', 'lock')
962
def __init__(self, iterable):
963
if isinstance(iterable, _concurrent_tee):
964
self.iterator = iterable.iterator
965
self.link = iterable.link
966
self.lock = iterable.lock
968
self.iterator = iter(iterable)
969
self.link = [None, None]
980
link[0] = next(self.iterator)
981
link[1] = [None, None]
982
value, self.link = link
985
############################################################
988
# Helper to generate new thread names
989
_counter = _count(1).__next__
990
def _newname(name_template):
991
return name_template % _counter()
993
# Active thread administration.
995
# bpo-44422: Use a reentrant lock to allow reentrant calls to functions like
996
# threading.enumerate().
997
_active_limbo_lock = RLock()
998
_active = {} # maps thread id to Thread object
1000
_dangling = WeakSet()
1003
# Main class for threads
1006
"""A class that represents a thread of control.
1008
This class can be safely subclassed in a limited fashion. There are two ways
1009
to specify the activity: by passing a callable object to the constructor, or
1010
by overriding the run() method in a subclass.
1014
_initialized = False
1016
def __init__(self, group=None, target=None, name=None,
1017
args=(), kwargs=None, *, daemon=None, context=None):
1018
"""This constructor should always be called with keyword arguments. Arguments are:
1020
*group* should be None; reserved for future extension when a ThreadGroup
1021
class is implemented.
1023
*target* is the callable object to be invoked by the run()
1024
method. Defaults to None, meaning nothing is called.
1026
*name* is the thread name. By default, a unique name is constructed of
1027
the form "Thread-N" where N is a small decimal number.
1029
*args* is a list or tuple of arguments for the target invocation. Defaults to ().
1031
*kwargs* is a dictionary of keyword arguments for the target
1032
invocation. Defaults to {}.
1034
*context* is the contextvars.Context value to use for the thread.
1035
The default value is None, which means to check
1036
sys.flags.thread_inherit_context. If that flag is true, use a copy
1037
of the context of the caller. If false, use an empty context. To
1038
explicitly start with an empty context, pass a new instance of
1039
contextvars.Context(). To explicitly start with a copy of the current
1040
context, pass the value from contextvars.copy_context().
1042
If a subclass overrides the constructor, it must make sure to invoke
1043
the base class constructor (Thread.__init__()) before doing anything
1047
assert group is None, "group argument must be None for now"
1053
name = _newname("Thread-%d")
1054
if target is not None:
1056
target_name = target.__name__
1057
name += f" ({target_name})"
1058
except AttributeError:
1061
self._target = target
1064
self._kwargs = kwargs
1065
if daemon is not None:
1066
if daemon and not _daemon_threads_allowed():
1067
raise RuntimeError('daemon threads are disabled in this (sub)interpreter')
1068
self._daemonic = daemon
1070
self._daemonic = current_thread().daemon
1071
self._context = context
1073
if _HAVE_THREAD_NATIVE_ID:
1074
self._native_id = None
1075
self._os_thread_handle = _ThreadHandle()
1076
self._started = Event()
1077
self._initialized = True
1078
# Copy of sys.stderr used by self._invoke_excepthook()
1079
self._stderr = _sys.stderr
1080
self._invoke_excepthook = _make_invoke_excepthook()
1081
# For debugging and _after_fork()
1084
def _after_fork(self, new_ident=None):
1085
# Private! Called by threading._after_fork().
1086
self._started._at_fork_reinit()
1087
if new_ident is not None:
1088
# This thread is alive.
1089
self._ident = new_ident
1090
assert self._os_thread_handle.ident == new_ident
1091
if _HAVE_THREAD_NATIVE_ID:
1092
self._set_native_id()
1094
# Otherwise, the thread is dead, Jim. _PyThread_AfterFork()
1095
# already marked our handle done.
1099
assert self._initialized, "Thread.__init__() was not called"
1101
if self._started.is_set():
1103
if self._os_thread_handle.is_done():
1107
if self._ident is not None:
1108
status += " %s" % self._ident
1109
return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
1112
"""Start the thread's activity.
1114
It must be called at most once per thread object. It arranges for the
1115
object's run() method to be invoked in a separate thread of control.
1117
This method will raise a RuntimeError if called more than once on the
1121
if not self._initialized:
1122
raise RuntimeError("thread.__init__() not called")
1124
if self._started.is_set():
1125
raise RuntimeError("threads can only be started once")
1127
with _active_limbo_lock:
1130
if self._context is None:
1131
# No context provided
1132
if _sys.flags.thread_inherit_context:
1133
# start with a copy of the context of the caller
1134
self._context = _contextvars.copy_context()
1136
# start with an empty context
1137
self._context = _contextvars.Context()
1140
# Start joinable thread
1141
_start_joinable_thread(self._bootstrap, handle=self._os_thread_handle,
1144
with _active_limbo_lock:
1147
self._started.wait() # Will set ident and native_id
1150
"""Method representing the thread's activity.
1152
You may override this method in a subclass. The standard run() method
1153
invokes the callable object passed to the object's constructor as the
1154
target argument, if any, with sequential and keyword arguments taken
1155
from the args and kwargs arguments, respectively.
1159
if self._target is not None:
1160
self._target(*self._args, **self._kwargs)
1162
# Avoid a refcycle if the thread is running a function with
1163
# an argument that has a member that points to the thread.
1164
del self._target, self._args, self._kwargs
1166
def _bootstrap(self):
1167
# Wrapper around the real bootstrap code that ignores
1168
# exceptions during interpreter cleanup. Those typically
1169
# happen when a daemon thread wakes up at an unfortunate
1170
# moment, finds the world around it destroyed, and raises some
1171
# random exception *** while trying to report the exception in
1172
# _bootstrap_inner() below ***. Those random exceptions
1173
# don't help anybody, and they confuse users, so we suppress
1174
# them. We suppress them only when it appears that the world
1175
# indeed has already been destroyed, so that exceptions in
1176
# _bootstrap_inner() during normal business hours are properly
1177
# reported. Also, we only suppress them for daemonic threads;
1178
# if a non-daemonic encounters this, something else is wrong.
1180
14546
self._bootstrap_inner()
1182
if self._daemonic and _sys is None:
1186
def _set_ident(self):
1187
self._ident = get_ident()
1189
if _HAVE_THREAD_NATIVE_ID:
1190
def _set_native_id(self):
1191
self._native_id = get_native_id()
1193
def _set_os_name(self):
1194
if _set_name is None or not self._name:
1197
_set_name(self._name)
1201
def _bootstrap_inner(self):
1204
if _HAVE_THREAD_NATIVE_ID:
1205
self._set_native_id()
1208
with _active_limbo_lock:
1209
_active[self._ident] = self
1213
_sys.settrace(_trace_hook)
1215
_sys.setprofile(_profile_hook)
1218
14546
self._context.run(self.run)
1220
self._invoke_excepthook(self)
1225
"Remove current thread from the dict of currently running threads."
1226
with _active_limbo_lock:
1227
del _active[get_ident()]
1228
# There must not be any python code between the previous line
1229
# and after the lock is released. Otherwise a tracing function
1230
# could try to acquire the lock again in the same thread, (in
1231
# current_thread()), and would block.
1233
def join(self, timeout=None):
1234
"""Wait until the thread terminates.
1236
This blocks the calling thread until the thread whose join() method is
1237
called terminates -- either normally or through an unhandled exception
1238
or until the optional timeout occurs.
1240
When the timeout argument is present and not None, it should be a
1241
floating-point number specifying a timeout for the operation in seconds
1242
(or fractions thereof). As join() always returns None, you must call
1243
is_alive() after join() to decide whether a timeout happened -- if the
1244
thread is still alive, the join() call timed out.
1246
When the timeout argument is not present or None, the operation will
1247
block until the thread terminates.
1249
A thread can be join()ed many times.
1251
join() raises a RuntimeError if an attempt is made to join the current
1252
thread as that would cause a deadlock. It is also an error to join() a
1253
thread before it has been started and attempts to do so raises the same
1257
if not self._initialized:
1258
raise RuntimeError("Thread.__init__() not called")
1259
if not self._started.is_set():
1260
raise RuntimeError("cannot join thread before it is started")
1261
if self is current_thread():
1262
raise RuntimeError("cannot join current thread")
1264
# the behavior of a negative timeout isn't documented, but
1265
# historically .join(timeout=x) for x<0 has acted as if timeout=0
1266
if timeout is not None:
1267
timeout = max(timeout, 0)
1269
self._os_thread_handle.join(timeout)
1273
"""A string used for identification purposes only.
1275
It has no semantics. Multiple threads may be given the same name. The
1276
initial name is set by the constructor.
1279
assert self._initialized, "Thread.__init__() not called"
1283
def name(self, name):
1284
assert self._initialized, "Thread.__init__() not called"
1285
self._name = str(name)
1286
if get_ident() == self._ident:
1291
"""Thread identifier of this thread or None if it has not been started.
1293
This is a nonzero integer. See the get_ident() function. Thread
1294
identifiers may be recycled when a thread exits and another thread is
1295
created. The identifier is available even after the thread has exited.
1298
assert self._initialized, "Thread.__init__() not called"
1301
if _HAVE_THREAD_NATIVE_ID:
1303
def native_id(self):
1304
"""Native integral thread ID of this thread, or None if it has not been started.
1306
This is a non-negative integer. See the get_native_id() function.
1307
This represents the Thread ID as reported by the kernel.
1310
assert self._initialized, "Thread.__init__() not called"
1311
return self._native_id
1314
"""Return whether the thread is alive.
1316
This method returns True just before the run() method starts until just
1317
after the run() method terminates. See also the module function
1321
assert self._initialized, "Thread.__init__() not called"
1322
return self._started.is_set() and not self._os_thread_handle.is_done()
1326
"""A boolean value indicating whether this thread is a daemon thread.
1328
This must be set before start() is called, otherwise RuntimeError is
1329
raised. Its initial value is inherited from the creating thread; the
1330
main thread is not a daemon thread and therefore all threads created in
1331
the main thread default to daemon = False.
1333
The entire Python program exits when only daemon threads are left.
1336
assert self._initialized, "Thread.__init__() not called"
1337
return self._daemonic
1340
def daemon(self, daemonic):
1341
if not self._initialized:
1342
raise RuntimeError("Thread.__init__() not called")
1343
if daemonic and not _daemon_threads_allowed():
1344
raise RuntimeError('daemon threads are disabled in this interpreter')
1345
if self._started.is_set():
1346
raise RuntimeError("cannot set daemon status of active thread")
1347
self._daemonic = daemonic
1350
"""Return whether this thread is a daemon.
1352
This method is deprecated, use the daemon attribute instead.
1356
warnings.warn('isDaemon() is deprecated, get the daemon attribute instead',
1357
DeprecationWarning, stacklevel=2)
1360
def setDaemon(self, daemonic):
1361
"""Set whether this thread is a daemon.
1363
This method is deprecated, use the .daemon property instead.
1367
warnings.warn('setDaemon() is deprecated, set the daemon attribute instead',
1368
DeprecationWarning, stacklevel=2)
1369
self.daemon = daemonic
1372
"""Return a string used for identification purposes only.
1374
This method is deprecated, use the name attribute instead.
1378
warnings.warn('getName() is deprecated, get the name attribute instead',
1379
DeprecationWarning, stacklevel=2)
1382
def setName(self, name):
1383
"""Set the name string for this thread.
1385
This method is deprecated, use the name attribute instead.
1389
warnings.warn('setName() is deprecated, set the name attribute instead',
1390
DeprecationWarning, stacklevel=2)
1395
from _thread import (_excepthook as excepthook,
1396
_ExceptHookArgs as ExceptHookArgs)
1398
# Simple Python implementation if _thread._excepthook() is not available
1399
from traceback import print_exception as _print_exception
1400
from collections import namedtuple
1402
_ExceptHookArgs = namedtuple(
1404
'exc_type exc_value exc_traceback thread')
1406
def ExceptHookArgs(args):
1407
return _ExceptHookArgs(*args)
1409
def excepthook(args, /):
1411
Handle uncaught Thread.run() exception.
1413
if args.exc_type == SystemExit:
1414
# silently ignore SystemExit
1417
if _sys is not None and _sys.stderr is not None:
1418
stderr = _sys.stderr
1419
elif args.thread is not None:
1420
stderr = args.thread._stderr
1422
# do nothing if sys.stderr is None and sys.stderr was None
1423
# when the thread was created
1426
# do nothing if sys.stderr is None and args.thread is None
1429
if args.thread is not None:
1430
name = args.thread.name
1433
print(f"Exception in thread {name}:",
1434
file=stderr, flush=True)
1435
_print_exception(args.exc_type, args.exc_value, args.exc_traceback,
1440
# Original value of threading.excepthook
1441
__excepthook__ = excepthook
1444
def _make_invoke_excepthook():
1445
# Create a local namespace to ensure that variables remain alive
1446
# when _invoke_excepthook() is called, even if it is called late during
1447
# Python shutdown. It is mostly needed for daemon threads.
1449
old_excepthook = excepthook
1450
old_sys_excepthook = _sys.excepthook
1451
if old_excepthook is None:
1452
raise RuntimeError("threading.excepthook is None")
1453
if old_sys_excepthook is None:
1454
raise RuntimeError("sys.excepthook is None")
1456
sys_exc_info = _sys.exc_info
1460
def invoke_excepthook(thread):
1465
hook = old_excepthook
1467
args = ExceptHookArgs([*sys_exc_info(), thread])
1470
except Exception as exc:
1471
exc.__suppress_context__ = True
1474
if local_sys is not None and local_sys.stderr is not None:
1475
stderr = local_sys.stderr
1477
stderr = thread._stderr
1479
local_print("Exception in threading.excepthook:",
1480
file=stderr, flush=True)
1482
if local_sys is not None and local_sys.excepthook is not None:
1483
sys_excepthook = local_sys.excepthook
1485
sys_excepthook = old_sys_excepthook
1487
sys_excepthook(*sys_exc_info())
1489
# Break reference cycle (exception stored in a variable)
1492
return invoke_excepthook
1495
# The timer class was contributed by Itamar Shtull-Trauring
1497
class Timer(Thread):
1498
"""Call a function after a specified number of seconds:
1500
t = Timer(30.0, f, args=None, kwargs=None)
1502
t.cancel() # stop the timer's action if it's still waiting
1506
def __init__(self, interval, function, args=None, kwargs=None):
1507
Thread.__init__(self)
1508
self.interval = interval
1509
self.function = function
1510
self.args = args if args is not None else []
1511
self.kwargs = kwargs if kwargs is not None else {}
1512
self.finished = Event()
1515
"""Stop the timer if it hasn't finished yet."""
1519
self.finished.wait(self.interval)
1520
if not self.finished.is_set():
1521
self.function(*self.args, **self.kwargs)
1525
# Special thread class to represent the main thread
1527
class _MainThread(Thread):
1530
Thread.__init__(self, name="MainThread", daemon=False)
1532
self._ident = _get_main_thread_ident()
1533
self._os_thread_handle = _make_thread_handle(self._ident)
1534
if _HAVE_THREAD_NATIVE_ID:
1535
self._set_native_id()
1536
with _active_limbo_lock:
1537
_active[self._ident] = self
1540
# Helper thread-local instance to detect when a _DummyThread
1541
# is collected. Not a part of the public API.
1542
_thread_local_info = local()
1545
class _DeleteDummyThreadOnDel:
1547
Helper class to remove a dummy thread from threading._active on __del__.
1550
def __init__(self, dummy_thread):
1551
self._dummy_thread = dummy_thread
1552
self._tident = dummy_thread.ident
1553
# Put the thread on a thread local variable so that when
1554
# the related thread finishes this instance is collected.
1556
# Note: no other references to this instance may be created.
1557
# If any client code creates a reference to this instance,
1558
# the related _DummyThread will be kept forever!
1559
_thread_local_info._track_dummy_thread_ref = self
1561
def __del__(self, _active_limbo_lock=_active_limbo_lock, _active=_active):
1562
with _active_limbo_lock:
1563
if _active.get(self._tident) is self._dummy_thread:
1564
_active.pop(self._tident, None)
1567
# Dummy thread class to represent threads not started here.
1568
# These should be added to `_active` and removed automatically
1569
# when they die, although they can't be waited for.
1570
# Their purpose is to return *something* from current_thread().
1571
# They are marked as daemon threads so we won't wait for them
1572
# when we exit (conform previous semantics).
1574
class _DummyThread(Thread):
1577
Thread.__init__(self, name=_newname("Dummy-%d"),
1578
daemon=_daemon_threads_allowed())
1581
self._os_thread_handle = _make_thread_handle(self._ident)
1582
if _HAVE_THREAD_NATIVE_ID:
1583
self._set_native_id()
1584
with _active_limbo_lock:
1585
_active[self._ident] = self
1586
_DeleteDummyThreadOnDel(self)
1589
if not self._os_thread_handle.is_done() and self._started.is_set():
1591
raise RuntimeError("thread is not alive")
1593
def join(self, timeout=None):
1594
raise RuntimeError("cannot join a dummy thread")
1596
def _after_fork(self, new_ident=None):
1597
if new_ident is not None:
1598
self.__class__ = _MainThread
1599
self._name = 'MainThread'
1600
self._daemonic = False
1601
Thread._after_fork(self, new_ident=new_ident)
1604
# Global API functions
1606
def current_thread():
1607
"""Return the current Thread object, corresponding to the caller's thread of control.
1609
If the caller's thread of control was not created through the threading
1610
module, a dummy thread object with limited functionality is returned.
1614
return _active[get_ident()]
1616
return _DummyThread()
1618
def currentThread():
1619
"""Return the current Thread object, corresponding to the caller's thread of control.
1621
This function is deprecated, use current_thread() instead.
1625
warnings.warn('currentThread() is deprecated, use current_thread() instead',
1626
DeprecationWarning, stacklevel=2)
1627
return current_thread()
1630
"""Return the number of Thread objects currently alive.
1632
The returned count is equal to the length of the list returned by
1636
# NOTE: if the logic in here ever changes, update Modules/posixmodule.c
1637
# warn_about_fork_with_threads() to match.
1638
with _active_limbo_lock:
1639
return len(_active) + len(_limbo)
1642
"""Return the number of Thread objects currently alive.
1644
This function is deprecated, use active_count() instead.
1648
warnings.warn('activeCount() is deprecated, use active_count() instead',
1649
DeprecationWarning, stacklevel=2)
1650
return active_count()
1653
# Same as enumerate(), but without the lock. Internal use only.
1654
return list(_active.values()) + list(_limbo.values())
1657
"""Return a list of all Thread objects currently alive.
1659
The list includes daemonic threads, dummy thread objects created by
1660
current_thread(), and the main thread. It excludes terminated threads and
1661
threads that have not yet been started.
1664
with _active_limbo_lock:
1665
return list(_active.values()) + list(_limbo.values())
1668
_threading_atexits = []
1669
_SHUTTING_DOWN = False
1671
def _register_atexit(func, *arg, **kwargs):
1672
"""CPython internal: register *func* to be called before joining threads.
1674
The registered *func* is called with its arguments just before all
1675
non-daemon threads are joined in `_shutdown()`. It provides a similar
1676
purpose to `atexit.register()`, but its functions are called prior to
1677
threading shutdown instead of interpreter shutdown.
1679
For similarity to atexit, the registered functions are called in reverse.
1682
raise RuntimeError("can't register atexit after shutdown")
1684
_threading_atexits.append(lambda: func(*arg, **kwargs))
1687
from _thread import stack_size
1689
# Create the main thread object,
1690
# and make it available for the interpreter
1691
# (Py_Main) as threading._shutdown.
1693
_main_thread = _MainThread()
1697
Wait until the Python thread state of all non-daemon threads get deleted.
1699
# Obscure: other threads may be waiting to join _main_thread. That's
1700
# dubious, but some code does it. We can't wait for it to be marked as done
1701
# normally - that won't happen until the interpreter is nearly dead. So
1702
# mark it done here.
1703
if _main_thread._os_thread_handle.is_done() and _is_main_interpreter():
1704
# _shutdown() was already called, but threads might have started
1706
return _thread_shutdown()
1708
global _SHUTTING_DOWN
1709
_SHUTTING_DOWN = True
1711
# Call registered threading atexit functions before threads are joined.
1712
# Order is reversed, similar to atexit.
1713
for atexit_call in reversed(_threading_atexits):
1716
if _is_main_interpreter():
1717
_main_thread._os_thread_handle._set_done()
1719
# Wait for all non-daemon threads to exit.
1724
"""Return the main thread object.
1726
In normal conditions, the main thread is the thread from which the
1727
Python interpreter was started.
1729
# XXX Figure this out for subinterpreters. (See gh-75698.)
1735
Cleanup threading module state that should not exist after a fork.
1737
# Reset _active_limbo_lock, in case we forked while the lock was held
1738
# by another (non-forked) thread. http://bugs.python.org/issue874900
1739
global _active_limbo_lock, _main_thread
1740
_active_limbo_lock = RLock()
1742
# fork() only copied the current thread; clear references to others.
1746
current = _active[get_ident()]
1748
# fork() was called in a thread which was not spawned
1749
# by threading.Thread. For example, a thread spawned
1750
# by thread.start_new_thread().
1751
current = _MainThread()
1753
_main_thread = current
1755
with _active_limbo_lock:
1756
# Dangling thread instances must still have their locks reset,
1757
# because someone may join() them.
1758
threads = set(_enumerate())
1759
threads.update(_dangling)
1760
for thread in threads:
1761
# Any lock/condition variable may be currently locked or in an
1762
# invalid state, so we reinitialize them.
1763
if thread is current:
1764
# This is the one and only active thread.
1766
thread._after_fork(new_ident=ident)
1767
new_active[ident] = thread
1769
# All the others are already stopped.
1770
thread._after_fork()
1774
_active.update(new_active)
1775
assert len(_active) == 1
1778
if hasattr(_os, "register_at_fork"):
1779
_os.register_at_fork(after_in_child=_after_fork)