threading.py 59 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708
  1. """Thread module emulating a subset of Java's threading model."""
  2. import os as _os
  3. import sys as _sys
  4. import _thread
  5. import functools
  6. from time import monotonic as _time
  7. from _weakrefset import WeakSet
  8. from itertools import count as _count
  9. try:
  10. from _collections import deque as _deque
  11. except ImportError:
  12. from collections import deque as _deque
  13. # Note regarding PEP 8 compliant names
  14. # This threading model was originally inspired by Java, and inherited
  15. # the convention of camelCase function and method names from that
  16. # language. Those original names are not in any imminent danger of
  17. # being deprecated (even for Py3k),so this module provides them as an
  18. # alias for the PEP 8 compliant names
  19. # Note that using the new PEP 8 compliant names facilitates substitution
  20. # with the multiprocessing module, which doesn't provide the old
  21. # Java inspired names.
  22. __all__ = ['get_ident', 'active_count', 'Condition', 'current_thread',
  23. 'enumerate', 'main_thread', 'TIMEOUT_MAX',
  24. 'Event', 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
  25. 'Barrier', 'BrokenBarrierError', 'Timer', 'ThreadError',
  26. 'setprofile', 'settrace', 'local', 'stack_size',
  27. 'excepthook', 'ExceptHookArgs', 'gettrace', 'getprofile',
  28. 'setprofile_all_threads','settrace_all_threads']
  29. # Rename some stuff so "from threading import *" is safe
  30. _start_new_thread = _thread.start_new_thread
  31. _daemon_threads_allowed = _thread.daemon_threads_allowed
  32. _allocate_lock = _thread.allocate_lock
  33. _set_sentinel = _thread._set_sentinel
  34. get_ident = _thread.get_ident
  35. try:
  36. _is_main_interpreter = _thread._is_main_interpreter
  37. except AttributeError:
  38. # See https://github.com/python/cpython/issues/112826.
  39. # We can pretend a subinterpreter is the main interpreter for the
  40. # sake of _shutdown(), since that only means we do not wait for the
  41. # subinterpreter's threads to finish. Instead, they will be stopped
  42. # later by the mechanism we use for daemon threads. The likelihood
  43. # of this case is small because rarely will the _thread module be
  44. # replaced by a module without _is_main_interpreter().
  45. # Furthermore, this is all irrelevant in applications
  46. # that do not use subinterpreters.
  47. def _is_main_interpreter():
  48. return True
  49. try:
  50. get_native_id = _thread.get_native_id
  51. _HAVE_THREAD_NATIVE_ID = True
  52. __all__.append('get_native_id')
  53. except AttributeError:
  54. _HAVE_THREAD_NATIVE_ID = False
  55. ThreadError = _thread.error
  56. try:
  57. _CRLock = _thread.RLock
  58. except AttributeError:
  59. _CRLock = None
  60. TIMEOUT_MAX = _thread.TIMEOUT_MAX
  61. del _thread
  62. # Support for profile and trace hooks
  63. _profile_hook = None
  64. _trace_hook = None
  65. def setprofile(func):
  66. """Set a profile function for all threads started from the threading module.
  67. The func will be passed to sys.setprofile() for each thread, before its
  68. run() method is called.
  69. """
  70. global _profile_hook
  71. _profile_hook = func
  72. def setprofile_all_threads(func):
  73. """Set a profile function for all threads started from the threading module
  74. and all Python threads that are currently executing.
  75. The func will be passed to sys.setprofile() for each thread, before its
  76. run() method is called.
  77. """
  78. setprofile(func)
  79. _sys._setprofileallthreads(func)
  80. def getprofile():
  81. """Get the profiler function as set by threading.setprofile()."""
  82. return _profile_hook
  83. def settrace(func):
  84. """Set a trace function for all threads started from the threading module.
  85. The func will be passed to sys.settrace() for each thread, before its run()
  86. method is called.
  87. """
  88. global _trace_hook
  89. _trace_hook = func
  90. def settrace_all_threads(func):
  91. """Set a trace function for all threads started from the threading module
  92. and all Python threads that are currently executing.
  93. The func will be passed to sys.settrace() for each thread, before its run()
  94. method is called.
  95. """
  96. settrace(func)
  97. _sys._settraceallthreads(func)
  98. def gettrace():
  99. """Get the trace function as set by threading.settrace()."""
  100. return _trace_hook
  101. # Synchronization classes
  102. Lock = _allocate_lock
  103. def RLock(*args, **kwargs):
  104. """Factory function that returns a new reentrant lock.
  105. A reentrant lock must be released by the thread that acquired it. Once a
  106. thread has acquired a reentrant lock, the same thread may acquire it again
  107. without blocking; the thread must release it once for each time it has
  108. acquired it.
  109. """
  110. if _CRLock is None:
  111. return _PyRLock(*args, **kwargs)
  112. return _CRLock(*args, **kwargs)
  113. class _RLock:
  114. """This class implements reentrant lock objects.
  115. A reentrant lock must be released by the thread that acquired it. Once a
  116. thread has acquired a reentrant lock, the same thread may acquire it
  117. again without blocking; the thread must release it once for each time it
  118. has acquired it.
  119. """
  120. def __init__(self):
  121. self._block = _allocate_lock()
  122. self._owner = None
  123. self._count = 0
  124. def __repr__(self):
  125. owner = self._owner
  126. try:
  127. owner = _active[owner].name
  128. except KeyError:
  129. pass
  130. return "<%s %s.%s object owner=%r count=%d at %s>" % (
  131. "locked" if self._block.locked() else "unlocked",
  132. self.__class__.__module__,
  133. self.__class__.__qualname__,
  134. owner,
  135. self._count,
  136. hex(id(self))
  137. )
  138. def _at_fork_reinit(self):
  139. self._block._at_fork_reinit()
  140. self._owner = None
  141. self._count = 0
  142. def acquire(self, blocking=True, timeout=-1):
  143. """Acquire a lock, blocking or non-blocking.
  144. When invoked without arguments: if this thread already owns the lock,
  145. increment the recursion level by one, and return immediately. Otherwise,
  146. if another thread owns the lock, block until the lock is unlocked. Once
  147. the lock is unlocked (not owned by any thread), then grab ownership, set
  148. the recursion level to one, and return. If more than one thread is
  149. blocked waiting until the lock is unlocked, only one at a time will be
  150. able to grab ownership of the lock. There is no return value in this
  151. case.
  152. When invoked with the blocking argument set to true, do the same thing
  153. as when called without arguments, and return true.
  154. When invoked with the blocking argument set to false, do not block. If a
  155. call without an argument would block, return false immediately;
  156. otherwise, do the same thing as when called without arguments, and
  157. return true.
  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. me = get_ident()
  164. if self._owner == me:
  165. self._count += 1
  166. return 1
  167. rc = self._block.acquire(blocking, timeout)
  168. if rc:
  169. self._owner = me
  170. self._count = 1
  171. return rc
  172. __enter__ = acquire
  173. def release(self):
  174. """Release a lock, decrementing the recursion level.
  175. If after the decrement it is zero, reset the lock to unlocked (not owned
  176. by any thread), and if any other threads are blocked waiting for the
  177. lock to become unlocked, allow exactly one of them to proceed. If after
  178. the decrement the recursion level is still nonzero, the lock remains
  179. locked and owned by the calling thread.
  180. Only call this method when the calling thread owns the lock. A
  181. RuntimeError is raised if this method is called when the lock is
  182. unlocked.
  183. There is no return value.
  184. """
  185. if self._owner != get_ident():
  186. raise RuntimeError("cannot release un-acquired lock")
  187. self._count = count = self._count - 1
  188. if not count:
  189. self._owner = None
  190. self._block.release()
  191. def __exit__(self, t, v, tb):
  192. self.release()
  193. # Internal methods used by condition variables
  194. def _acquire_restore(self, state):
  195. self._block.acquire()
  196. self._count, self._owner = state
  197. def _release_save(self):
  198. if self._count == 0:
  199. raise RuntimeError("cannot release un-acquired lock")
  200. count = self._count
  201. self._count = 0
  202. owner = self._owner
  203. self._owner = None
  204. self._block.release()
  205. return (count, owner)
  206. def _is_owned(self):
  207. return self._owner == get_ident()
  208. # Internal method used for reentrancy checks
  209. def _recursion_count(self):
  210. if self._owner != get_ident():
  211. return 0
  212. return self._count
  213. _PyRLock = _RLock
  214. class Condition:
  215. """Class that implements a condition variable.
  216. A condition variable allows one or more threads to wait until they are
  217. notified by another thread.
  218. If the lock argument is given and not None, it must be a Lock or RLock
  219. object, and it is used as the underlying lock. Otherwise, a new RLock object
  220. is created and used as the underlying lock.
  221. """
  222. def __init__(self, lock=None):
  223. if lock is None:
  224. lock = RLock()
  225. self._lock = lock
  226. # Export the lock's acquire() and release() methods
  227. self.acquire = lock.acquire
  228. self.release = lock.release
  229. # If the lock defines _release_save() and/or _acquire_restore(),
  230. # these override the default implementations (which just call
  231. # release() and acquire() on the lock). Ditto for _is_owned().
  232. if hasattr(lock, '_release_save'):
  233. self._release_save = lock._release_save
  234. if hasattr(lock, '_acquire_restore'):
  235. self._acquire_restore = lock._acquire_restore
  236. if hasattr(lock, '_is_owned'):
  237. self._is_owned = lock._is_owned
  238. self._waiters = _deque()
  239. def _at_fork_reinit(self):
  240. self._lock._at_fork_reinit()
  241. self._waiters.clear()
  242. def __enter__(self):
  243. return self._lock.__enter__()
  244. def __exit__(self, *args):
  245. return self._lock.__exit__(*args)
  246. def __repr__(self):
  247. return "<Condition(%s, %d)>" % (self._lock, len(self._waiters))
  248. def _release_save(self):
  249. self._lock.release() # No state to save
  250. def _acquire_restore(self, x):
  251. self._lock.acquire() # Ignore saved state
  252. def _is_owned(self):
  253. # Return True if lock is owned by current_thread.
  254. # This method is called only if _lock doesn't have _is_owned().
  255. if self._lock.acquire(False):
  256. self._lock.release()
  257. return False
  258. else:
  259. return True
  260. def wait(self, timeout=None):
  261. """Wait until notified or until a timeout occurs.
  262. If the calling thread has not acquired the lock when this method is
  263. called, a RuntimeError is raised.
  264. This method releases the underlying lock, and then blocks until it is
  265. awakened by a notify() or notify_all() call for the same condition
  266. variable in another thread, or until the optional timeout occurs. Once
  267. awakened or timed out, it re-acquires the lock and returns.
  268. When the timeout argument is present and not None, it should be a
  269. floating-point number specifying a timeout for the operation in seconds
  270. (or fractions thereof).
  271. When the underlying lock is an RLock, it is not released using its
  272. release() method, since this may not actually unlock the lock when it
  273. was acquired multiple times recursively. Instead, an internal interface
  274. of the RLock class is used, which really unlocks it even when it has
  275. been recursively acquired several times. Another internal interface is
  276. then used to restore the recursion level when the lock is reacquired.
  277. """
  278. if not self._is_owned():
  279. raise RuntimeError("cannot wait on un-acquired lock")
  280. waiter = _allocate_lock()
  281. waiter.acquire()
  282. self._waiters.append(waiter)
  283. saved_state = self._release_save()
  284. gotit = False
  285. try: # restore state no matter what (e.g., KeyboardInterrupt)
  286. if timeout is None:
  287. waiter.acquire()
  288. gotit = True
  289. else:
  290. if timeout > 0:
  291. gotit = waiter.acquire(True, timeout)
  292. else:
  293. gotit = waiter.acquire(False)
  294. return gotit
  295. finally:
  296. self._acquire_restore(saved_state)
  297. if not gotit:
  298. try:
  299. self._waiters.remove(waiter)
  300. except ValueError:
  301. pass
  302. def wait_for(self, predicate, timeout=None):
  303. """Wait until a condition evaluates to True.
  304. predicate should be a callable which result will be interpreted as a
  305. boolean value. A timeout may be provided giving the maximum time to
  306. wait.
  307. """
  308. endtime = None
  309. waittime = timeout
  310. result = predicate()
  311. while not result:
  312. if waittime is not None:
  313. if endtime is None:
  314. endtime = _time() + waittime
  315. else:
  316. waittime = endtime - _time()
  317. if waittime <= 0:
  318. break
  319. self.wait(waittime)
  320. result = predicate()
  321. return result
  322. def notify(self, n=1):
  323. """Wake up one or more threads waiting on this condition, if any.
  324. If the calling thread has not acquired the lock when this method is
  325. called, a RuntimeError is raised.
  326. This method wakes up at most n of the threads waiting for the condition
  327. variable; it is a no-op if no threads are waiting.
  328. """
  329. if not self._is_owned():
  330. raise RuntimeError("cannot notify on un-acquired lock")
  331. waiters = self._waiters
  332. while waiters and n > 0:
  333. waiter = waiters[0]
  334. try:
  335. waiter.release()
  336. except RuntimeError:
  337. # gh-92530: The previous call of notify() released the lock,
  338. # but was interrupted before removing it from the queue.
  339. # It can happen if a signal handler raises an exception,
  340. # like CTRL+C which raises KeyboardInterrupt.
  341. pass
  342. else:
  343. n -= 1
  344. try:
  345. waiters.remove(waiter)
  346. except ValueError:
  347. pass
  348. def notify_all(self):
  349. """Wake up all threads waiting on this condition.
  350. If the calling thread has not acquired the lock when this method
  351. is called, a RuntimeError is raised.
  352. """
  353. self.notify(len(self._waiters))
  354. def notifyAll(self):
  355. """Wake up all threads waiting on this condition.
  356. This method is deprecated, use notify_all() instead.
  357. """
  358. import warnings
  359. warnings.warn('notifyAll() is deprecated, use notify_all() instead',
  360. DeprecationWarning, stacklevel=2)
  361. self.notify_all()
  362. class Semaphore:
  363. """This class implements semaphore objects.
  364. Semaphores manage a counter representing the number of release() calls minus
  365. the number of acquire() calls, plus an initial value. The acquire() method
  366. blocks if necessary until it can return without making the counter
  367. negative. If not given, value defaults to 1.
  368. """
  369. # After Tim Peters' semaphore class, but not quite the same (no maximum)
  370. def __init__(self, value=1):
  371. if value < 0:
  372. raise ValueError("semaphore initial value must be >= 0")
  373. self._cond = Condition(Lock())
  374. self._value = value
  375. def __repr__(self):
  376. cls = self.__class__
  377. return (f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}:"
  378. f" value={self._value}>")
  379. def acquire(self, blocking=True, timeout=None):
  380. """Acquire a semaphore, decrementing the internal counter by one.
  381. When invoked without arguments: if the internal counter is larger than
  382. zero on entry, decrement it by one and return immediately. If it is zero
  383. on entry, block, waiting until some other thread has called release() to
  384. make it larger than zero. This is done with proper interlocking so that
  385. if multiple acquire() calls are blocked, release() will wake exactly one
  386. of them up. The implementation may pick one at random, so the order in
  387. which blocked threads are awakened should not be relied on. There is no
  388. return value in this case.
  389. When invoked with blocking set to true, do the same thing as when called
  390. without arguments, and return true.
  391. When invoked with blocking set to false, do not block. If a call without
  392. an argument would block, return false immediately; otherwise, do the
  393. same thing as when called without arguments, and return true.
  394. When invoked with a timeout other than None, it will block for at
  395. most timeout seconds. If acquire does not complete successfully in
  396. that interval, return false. Return true otherwise.
  397. """
  398. if not blocking and timeout is not None:
  399. raise ValueError("can't specify timeout for non-blocking acquire")
  400. rc = False
  401. endtime = None
  402. with self._cond:
  403. while self._value == 0:
  404. if not blocking:
  405. break
  406. if timeout is not None:
  407. if endtime is None:
  408. endtime = _time() + timeout
  409. else:
  410. timeout = endtime - _time()
  411. if timeout <= 0:
  412. break
  413. self._cond.wait(timeout)
  414. else:
  415. self._value -= 1
  416. rc = True
  417. return rc
  418. __enter__ = acquire
  419. def release(self, n=1):
  420. """Release a semaphore, incrementing the internal counter by one or more.
  421. When the counter is zero on entry and another thread is waiting for it
  422. to become larger than zero again, wake up that thread.
  423. """
  424. if n < 1:
  425. raise ValueError('n must be one or more')
  426. with self._cond:
  427. self._value += n
  428. self._cond.notify(n)
  429. def __exit__(self, t, v, tb):
  430. self.release()
  431. class BoundedSemaphore(Semaphore):
  432. """Implements a bounded semaphore.
  433. A bounded semaphore checks to make sure its current value doesn't exceed its
  434. initial value. If it does, ValueError is raised. In most situations
  435. semaphores are used to guard resources with limited capacity.
  436. If the semaphore is released too many times it's a sign of a bug. If not
  437. given, value defaults to 1.
  438. Like regular semaphores, bounded semaphores manage a counter representing
  439. the number of release() calls minus the number of acquire() calls, plus an
  440. initial value. The acquire() method blocks if necessary until it can return
  441. without making the counter negative. If not given, value defaults to 1.
  442. """
  443. def __init__(self, value=1):
  444. super().__init__(value)
  445. self._initial_value = value
  446. def __repr__(self):
  447. cls = self.__class__
  448. return (f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}:"
  449. f" value={self._value}/{self._initial_value}>")
  450. def release(self, n=1):
  451. """Release a semaphore, incrementing the internal counter by one or more.
  452. When the counter is zero on entry and another thread is waiting for it
  453. to become larger than zero again, wake up that thread.
  454. If the number of releases exceeds the number of acquires,
  455. raise a ValueError.
  456. """
  457. if n < 1:
  458. raise ValueError('n must be one or more')
  459. with self._cond:
  460. if self._value + n > self._initial_value:
  461. raise ValueError("Semaphore released too many times")
  462. self._value += n
  463. self._cond.notify(n)
  464. class Event:
  465. """Class implementing event objects.
  466. Events manage a flag that can be set to true with the set() method and reset
  467. to false with the clear() method. The wait() method blocks until the flag is
  468. true. The flag is initially false.
  469. """
  470. # After Tim Peters' event class (without is_posted())
  471. def __init__(self):
  472. self._cond = Condition(Lock())
  473. self._flag = False
  474. def __repr__(self):
  475. cls = self.__class__
  476. status = 'set' if self._flag else 'unset'
  477. return f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}: {status}>"
  478. def _at_fork_reinit(self):
  479. # Private method called by Thread._reset_internal_locks()
  480. self._cond._at_fork_reinit()
  481. def is_set(self):
  482. """Return true if and only if the internal flag is true."""
  483. return self._flag
  484. def isSet(self):
  485. """Return true if and only if the internal flag is true.
  486. This method is deprecated, use is_set() instead.
  487. """
  488. import warnings
  489. warnings.warn('isSet() is deprecated, use is_set() instead',
  490. DeprecationWarning, stacklevel=2)
  491. return self.is_set()
  492. def set(self):
  493. """Set the internal flag to true.
  494. All threads waiting for it to become true are awakened. Threads
  495. that call wait() once the flag is true will not block at all.
  496. """
  497. with self._cond:
  498. self._flag = True
  499. self._cond.notify_all()
  500. def clear(self):
  501. """Reset the internal flag to false.
  502. Subsequently, threads calling wait() will block until set() is called to
  503. set the internal flag to true again.
  504. """
  505. with self._cond:
  506. self._flag = False
  507. def wait(self, timeout=None):
  508. """Block until the internal flag is true.
  509. If the internal flag is true on entry, return immediately. Otherwise,
  510. block until another thread calls set() to set the flag to true, or until
  511. the optional timeout occurs.
  512. When the timeout argument is present and not None, it should be a
  513. floating-point number specifying a timeout for the operation in seconds
  514. (or fractions thereof).
  515. This method returns the internal flag on exit, so it will always return
  516. True except if a timeout is given and the operation times out.
  517. """
  518. with self._cond:
  519. signaled = self._flag
  520. if not signaled:
  521. signaled = self._cond.wait(timeout)
  522. return signaled
  523. # A barrier class. Inspired in part by the pthread_barrier_* api and
  524. # the CyclicBarrier class from Java. See
  525. # http://sourceware.org/pthreads-win32/manual/pthread_barrier_init.html and
  526. # http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/
  527. # CyclicBarrier.html
  528. # for information.
  529. # We maintain two main states, 'filling' and 'draining' enabling the barrier
  530. # to be cyclic. Threads are not allowed into it until it has fully drained
  531. # since the previous cycle. In addition, a 'resetting' state exists which is
  532. # similar to 'draining' except that threads leave with a BrokenBarrierError,
  533. # and a 'broken' state in which all threads get the exception.
  534. class Barrier:
  535. """Implements a Barrier.
  536. Useful for synchronizing a fixed number of threads at known synchronization
  537. points. Threads block on 'wait()' and are simultaneously awoken once they
  538. have all made that call.
  539. """
  540. def __init__(self, parties, action=None, timeout=None):
  541. """Create a barrier, initialised to 'parties' threads.
  542. 'action' is a callable which, when supplied, will be called by one of
  543. the threads after they have all entered the barrier and just prior to
  544. releasing them all. If a 'timeout' is provided, it is used as the
  545. default for all subsequent 'wait()' calls.
  546. """
  547. if parties < 1:
  548. raise ValueError("parties must be > 0")
  549. self._cond = Condition(Lock())
  550. self._action = action
  551. self._timeout = timeout
  552. self._parties = parties
  553. self._state = 0 # 0 filling, 1 draining, -1 resetting, -2 broken
  554. self._count = 0
  555. def __repr__(self):
  556. cls = self.__class__
  557. if self.broken:
  558. return f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}: broken>"
  559. return (f"<{cls.__module__}.{cls.__qualname__} at {id(self):#x}:"
  560. f" waiters={self.n_waiting}/{self.parties}>")
  561. def wait(self, timeout=None):
  562. """Wait for the barrier.
  563. When the specified number of threads have started waiting, they are all
  564. simultaneously awoken. If an 'action' was provided for the barrier, one
  565. of the threads will have executed that callback prior to returning.
  566. Returns an individual index number from 0 to 'parties-1'.
  567. """
  568. if timeout is None:
  569. timeout = self._timeout
  570. with self._cond:
  571. self._enter() # Block while the barrier drains.
  572. index = self._count
  573. self._count += 1
  574. try:
  575. if index + 1 == self._parties:
  576. # We release the barrier
  577. self._release()
  578. else:
  579. # We wait until someone releases us
  580. self._wait(timeout)
  581. return index
  582. finally:
  583. self._count -= 1
  584. # Wake up any threads waiting for barrier to drain.
  585. self._exit()
  586. # Block until the barrier is ready for us, or raise an exception
  587. # if it is broken.
  588. def _enter(self):
  589. while self._state in (-1, 1):
  590. # It is draining or resetting, wait until done
  591. self._cond.wait()
  592. #see if the barrier is in a broken state
  593. if self._state < 0:
  594. raise BrokenBarrierError
  595. assert self._state == 0
  596. # Optionally run the 'action' and release the threads waiting
  597. # in the barrier.
  598. def _release(self):
  599. try:
  600. if self._action:
  601. self._action()
  602. # enter draining state
  603. self._state = 1
  604. self._cond.notify_all()
  605. except:
  606. #an exception during the _action handler. Break and reraise
  607. self._break()
  608. raise
  609. # Wait in the barrier until we are released. Raise an exception
  610. # if the barrier is reset or broken.
  611. def _wait(self, timeout):
  612. if not self._cond.wait_for(lambda : self._state != 0, timeout):
  613. #timed out. Break the barrier
  614. self._break()
  615. raise BrokenBarrierError
  616. if self._state < 0:
  617. raise BrokenBarrierError
  618. assert self._state == 1
  619. # If we are the last thread to exit the barrier, signal any threads
  620. # waiting for the barrier to drain.
  621. def _exit(self):
  622. if self._count == 0:
  623. if self._state in (-1, 1):
  624. #resetting or draining
  625. self._state = 0
  626. self._cond.notify_all()
  627. def reset(self):
  628. """Reset the barrier to the initial state.
  629. Any threads currently waiting will get the BrokenBarrier exception
  630. raised.
  631. """
  632. with self._cond:
  633. if self._count > 0:
  634. if self._state == 0:
  635. #reset the barrier, waking up threads
  636. self._state = -1
  637. elif self._state == -2:
  638. #was broken, set it to reset state
  639. #which clears when the last thread exits
  640. self._state = -1
  641. else:
  642. self._state = 0
  643. self._cond.notify_all()
  644. def abort(self):
  645. """Place the barrier into a 'broken' state.
  646. Useful in case of error. Any currently waiting threads and threads
  647. attempting to 'wait()' will have BrokenBarrierError raised.
  648. """
  649. with self._cond:
  650. self._break()
  651. def _break(self):
  652. # An internal error was detected. The barrier is set to
  653. # a broken state all parties awakened.
  654. self._state = -2
  655. self._cond.notify_all()
  656. @property
  657. def parties(self):
  658. """Return the number of threads required to trip the barrier."""
  659. return self._parties
  660. @property
  661. def n_waiting(self):
  662. """Return the number of threads currently waiting at the barrier."""
  663. # We don't need synchronization here since this is an ephemeral result
  664. # anyway. It returns the correct value in the steady state.
  665. if self._state == 0:
  666. return self._count
  667. return 0
  668. @property
  669. def broken(self):
  670. """Return True if the barrier is in a broken state."""
  671. return self._state == -2
  672. # exception raised by the Barrier class
  673. class BrokenBarrierError(RuntimeError):
  674. pass
  675. # Helper to generate new thread names
  676. _counter = _count(1).__next__
  677. def _newname(name_template):
  678. return name_template % _counter()
  679. # Active thread administration.
  680. #
  681. # bpo-44422: Use a reentrant lock to allow reentrant calls to functions like
  682. # threading.enumerate().
  683. _active_limbo_lock = RLock()
  684. _active = {} # maps thread id to Thread object
  685. _limbo = {}
  686. _dangling = WeakSet()
  687. # Set of Thread._tstate_lock locks of non-daemon threads used by _shutdown()
  688. # to wait until all Python thread states get deleted:
  689. # see Thread._set_tstate_lock().
  690. _shutdown_locks_lock = _allocate_lock()
  691. _shutdown_locks = set()
  692. def _maintain_shutdown_locks():
  693. """
  694. Drop any shutdown locks that don't correspond to running threads anymore.
  695. Calling this from time to time avoids an ever-growing _shutdown_locks
  696. set when Thread objects are not joined explicitly. See bpo-37788.
  697. This must be called with _shutdown_locks_lock acquired.
  698. """
  699. # If a lock was released, the corresponding thread has exited
  700. to_remove = [lock for lock in _shutdown_locks if not lock.locked()]
  701. _shutdown_locks.difference_update(to_remove)
  702. # Main class for threads
  703. class Thread:
  704. """A class that represents a thread of control.
  705. This class can be safely subclassed in a limited fashion. There are two ways
  706. to specify the activity: by passing a callable object to the constructor, or
  707. by overriding the run() method in a subclass.
  708. """
  709. _initialized = False
  710. def __init__(self, group=None, target=None, name=None,
  711. args=(), kwargs=None, *, daemon=None):
  712. """This constructor should always be called with keyword arguments. Arguments are:
  713. *group* should be None; reserved for future extension when a ThreadGroup
  714. class is implemented.
  715. *target* is the callable object to be invoked by the run()
  716. method. Defaults to None, meaning nothing is called.
  717. *name* is the thread name. By default, a unique name is constructed of
  718. the form "Thread-N" where N is a small decimal number.
  719. *args* is a list or tuple of arguments for the target invocation. Defaults to ().
  720. *kwargs* is a dictionary of keyword arguments for the target
  721. invocation. Defaults to {}.
  722. If a subclass overrides the constructor, it must make sure to invoke
  723. the base class constructor (Thread.__init__()) before doing anything
  724. else to the thread.
  725. """
  726. assert group is None, "group argument must be None for now"
  727. if kwargs is None:
  728. kwargs = {}
  729. if name:
  730. name = str(name)
  731. else:
  732. name = _newname("Thread-%d")
  733. if target is not None:
  734. try:
  735. target_name = target.__name__
  736. name += f" ({target_name})"
  737. except AttributeError:
  738. pass
  739. self._target = target
  740. self._name = name
  741. self._args = args
  742. self._kwargs = kwargs
  743. if daemon is not None:
  744. if daemon and not _daemon_threads_allowed():
  745. raise RuntimeError('daemon threads are disabled in this (sub)interpreter')
  746. self._daemonic = daemon
  747. else:
  748. self._daemonic = current_thread().daemon
  749. self._ident = None
  750. if _HAVE_THREAD_NATIVE_ID:
  751. self._native_id = None
  752. self._tstate_lock = None
  753. self._started = Event()
  754. self._is_stopped = False
  755. self._initialized = True
  756. # Copy of sys.stderr used by self._invoke_excepthook()
  757. self._stderr = _sys.stderr
  758. self._invoke_excepthook = _make_invoke_excepthook()
  759. # For debugging and _after_fork()
  760. _dangling.add(self)
  761. def _reset_internal_locks(self, is_alive):
  762. # private! Called by _after_fork() to reset our internal locks as
  763. # they may be in an invalid state leading to a deadlock or crash.
  764. self._started._at_fork_reinit()
  765. if is_alive:
  766. # bpo-42350: If the fork happens when the thread is already stopped
  767. # (ex: after threading._shutdown() has been called), _tstate_lock
  768. # is None. Do nothing in this case.
  769. if self._tstate_lock is not None:
  770. self._tstate_lock._at_fork_reinit()
  771. self._tstate_lock.acquire()
  772. else:
  773. # The thread isn't alive after fork: it doesn't have a tstate
  774. # anymore.
  775. self._is_stopped = True
  776. self._tstate_lock = None
  777. def __repr__(self):
  778. assert self._initialized, "Thread.__init__() was not called"
  779. status = "initial"
  780. if self._started.is_set():
  781. status = "started"
  782. self.is_alive() # easy way to get ._is_stopped set when appropriate
  783. if self._is_stopped:
  784. status = "stopped"
  785. if self._daemonic:
  786. status += " daemon"
  787. if self._ident is not None:
  788. status += " %s" % self._ident
  789. return "<%s(%s, %s)>" % (self.__class__.__name__, self._name, status)
  790. def start(self):
  791. """Start the thread's activity.
  792. It must be called at most once per thread object. It arranges for the
  793. object's run() method to be invoked in a separate thread of control.
  794. This method will raise a RuntimeError if called more than once on the
  795. same thread object.
  796. """
  797. if not self._initialized:
  798. raise RuntimeError("thread.__init__() not called")
  799. if self._started.is_set():
  800. raise RuntimeError("threads can only be started once")
  801. with _active_limbo_lock:
  802. _limbo[self] = self
  803. try:
  804. _start_new_thread(self._bootstrap, ())
  805. except Exception:
  806. with _active_limbo_lock:
  807. del _limbo[self]
  808. raise
  809. self._started.wait()
  810. def run(self):
  811. """Method representing the thread's activity.
  812. You may override this method in a subclass. The standard run() method
  813. invokes the callable object passed to the object's constructor as the
  814. target argument, if any, with sequential and keyword arguments taken
  815. from the args and kwargs arguments, respectively.
  816. """
  817. try:
  818. if self._target is not None:
  819. self._target(*self._args, **self._kwargs)
  820. finally:
  821. # Avoid a refcycle if the thread is running a function with
  822. # an argument that has a member that points to the thread.
  823. del self._target, self._args, self._kwargs
  824. def _bootstrap(self):
  825. # Wrapper around the real bootstrap code that ignores
  826. # exceptions during interpreter cleanup. Those typically
  827. # happen when a daemon thread wakes up at an unfortunate
  828. # moment, finds the world around it destroyed, and raises some
  829. # random exception *** while trying to report the exception in
  830. # _bootstrap_inner() below ***. Those random exceptions
  831. # don't help anybody, and they confuse users, so we suppress
  832. # them. We suppress them only when it appears that the world
  833. # indeed has already been destroyed, so that exceptions in
  834. # _bootstrap_inner() during normal business hours are properly
  835. # reported. Also, we only suppress them for daemonic threads;
  836. # if a non-daemonic encounters this, something else is wrong.
  837. try:
  838. self._bootstrap_inner()
  839. except:
  840. if self._daemonic and _sys is None:
  841. return
  842. raise
  843. def _set_ident(self):
  844. self._ident = get_ident()
  845. if _HAVE_THREAD_NATIVE_ID:
  846. def _set_native_id(self):
  847. self._native_id = get_native_id()
  848. def _set_tstate_lock(self):
  849. """
  850. Set a lock object which will be released by the interpreter when
  851. the underlying thread state (see pystate.h) gets deleted.
  852. """
  853. self._tstate_lock = _set_sentinel()
  854. self._tstate_lock.acquire()
  855. if not self.daemon:
  856. with _shutdown_locks_lock:
  857. _maintain_shutdown_locks()
  858. _shutdown_locks.add(self._tstate_lock)
  859. def _bootstrap_inner(self):
  860. try:
  861. self._set_ident()
  862. self._set_tstate_lock()
  863. if _HAVE_THREAD_NATIVE_ID:
  864. self._set_native_id()
  865. self._started.set()
  866. with _active_limbo_lock:
  867. _active[self._ident] = self
  868. del _limbo[self]
  869. if _trace_hook:
  870. _sys.settrace(_trace_hook)
  871. if _profile_hook:
  872. _sys.setprofile(_profile_hook)
  873. try:
  874. self.run()
  875. except:
  876. self._invoke_excepthook(self)
  877. finally:
  878. self._delete()
  879. def _stop(self):
  880. # After calling ._stop(), .is_alive() returns False and .join() returns
  881. # immediately. ._tstate_lock must be released before calling ._stop().
  882. #
  883. # Normal case: C code at the end of the thread's life
  884. # (release_sentinel in _threadmodule.c) releases ._tstate_lock, and
  885. # that's detected by our ._wait_for_tstate_lock(), called by .join()
  886. # and .is_alive(). Any number of threads _may_ call ._stop()
  887. # simultaneously (for example, if multiple threads are blocked in
  888. # .join() calls), and they're not serialized. That's harmless -
  889. # they'll just make redundant rebindings of ._is_stopped and
  890. # ._tstate_lock. Obscure: we rebind ._tstate_lock last so that the
  891. # "assert self._is_stopped" in ._wait_for_tstate_lock() always works
  892. # (the assert is executed only if ._tstate_lock is None).
  893. #
  894. # Special case: _main_thread releases ._tstate_lock via this
  895. # module's _shutdown() function.
  896. lock = self._tstate_lock
  897. if lock is not None:
  898. assert not lock.locked()
  899. self._is_stopped = True
  900. self._tstate_lock = None
  901. if not self.daemon:
  902. with _shutdown_locks_lock:
  903. # Remove our lock and other released locks from _shutdown_locks
  904. _maintain_shutdown_locks()
  905. def _delete(self):
  906. "Remove current thread from the dict of currently running threads."
  907. with _active_limbo_lock:
  908. del _active[get_ident()]
  909. # There must not be any python code between the previous line
  910. # and after the lock is released. Otherwise a tracing function
  911. # could try to acquire the lock again in the same thread, (in
  912. # current_thread()), and would block.
  913. def join(self, timeout=None):
  914. """Wait until the thread terminates.
  915. This blocks the calling thread until the thread whose join() method is
  916. called terminates -- either normally or through an unhandled exception
  917. or until the optional timeout occurs.
  918. When the timeout argument is present and not None, it should be a
  919. floating-point number specifying a timeout for the operation in seconds
  920. (or fractions thereof). As join() always returns None, you must call
  921. is_alive() after join() to decide whether a timeout happened -- if the
  922. thread is still alive, the join() call timed out.
  923. When the timeout argument is not present or None, the operation will
  924. block until the thread terminates.
  925. A thread can be join()ed many times.
  926. join() raises a RuntimeError if an attempt is made to join the current
  927. thread as that would cause a deadlock. It is also an error to join() a
  928. thread before it has been started and attempts to do so raises the same
  929. exception.
  930. """
  931. if not self._initialized:
  932. raise RuntimeError("Thread.__init__() not called")
  933. if not self._started.is_set():
  934. raise RuntimeError("cannot join thread before it is started")
  935. if self is current_thread():
  936. raise RuntimeError("cannot join current thread")
  937. if timeout is None:
  938. self._wait_for_tstate_lock()
  939. else:
  940. # the behavior of a negative timeout isn't documented, but
  941. # historically .join(timeout=x) for x<0 has acted as if timeout=0
  942. self._wait_for_tstate_lock(timeout=max(timeout, 0))
  943. def _wait_for_tstate_lock(self, block=True, timeout=-1):
  944. # Issue #18808: wait for the thread state to be gone.
  945. # At the end of the thread's life, after all knowledge of the thread
  946. # is removed from C data structures, C code releases our _tstate_lock.
  947. # This method passes its arguments to _tstate_lock.acquire().
  948. # If the lock is acquired, the C code is done, and self._stop() is
  949. # called. That sets ._is_stopped to True, and ._tstate_lock to None.
  950. lock = self._tstate_lock
  951. if lock is None:
  952. # already determined that the C code is done
  953. assert self._is_stopped
  954. return
  955. try:
  956. if lock.acquire(block, timeout):
  957. lock.release()
  958. self._stop()
  959. except:
  960. if lock.locked():
  961. # bpo-45274: lock.acquire() acquired the lock, but the function
  962. # was interrupted with an exception before reaching the
  963. # lock.release(). It can happen if a signal handler raises an
  964. # exception, like CTRL+C which raises KeyboardInterrupt.
  965. lock.release()
  966. self._stop()
  967. raise
  968. @property
  969. def name(self):
  970. """A string used for identification purposes only.
  971. It has no semantics. Multiple threads may be given the same name. The
  972. initial name is set by the constructor.
  973. """
  974. assert self._initialized, "Thread.__init__() not called"
  975. return self._name
  976. @name.setter
  977. def name(self, name):
  978. assert self._initialized, "Thread.__init__() not called"
  979. self._name = str(name)
  980. @property
  981. def ident(self):
  982. """Thread identifier of this thread or None if it has not been started.
  983. This is a nonzero integer. See the get_ident() function. Thread
  984. identifiers may be recycled when a thread exits and another thread is
  985. created. The identifier is available even after the thread has exited.
  986. """
  987. assert self._initialized, "Thread.__init__() not called"
  988. return self._ident
  989. if _HAVE_THREAD_NATIVE_ID:
  990. @property
  991. def native_id(self):
  992. """Native integral thread ID of this thread, or None if it has not been started.
  993. This is a non-negative integer. See the get_native_id() function.
  994. This represents the Thread ID as reported by the kernel.
  995. """
  996. assert self._initialized, "Thread.__init__() not called"
  997. return self._native_id
  998. def is_alive(self):
  999. """Return whether the thread is alive.
  1000. This method returns True just before the run() method starts until just
  1001. after the run() method terminates. See also the module function
  1002. enumerate().
  1003. """
  1004. assert self._initialized, "Thread.__init__() not called"
  1005. if self._is_stopped or not self._started.is_set():
  1006. return False
  1007. self._wait_for_tstate_lock(False)
  1008. return not self._is_stopped
  1009. @property
  1010. def daemon(self):
  1011. """A boolean value indicating whether this thread is a daemon thread.
  1012. This must be set before start() is called, otherwise RuntimeError is
  1013. raised. Its initial value is inherited from the creating thread; the
  1014. main thread is not a daemon thread and therefore all threads created in
  1015. the main thread default to daemon = False.
  1016. The entire Python program exits when only daemon threads are left.
  1017. """
  1018. assert self._initialized, "Thread.__init__() not called"
  1019. return self._daemonic
  1020. @daemon.setter
  1021. def daemon(self, daemonic):
  1022. if not self._initialized:
  1023. raise RuntimeError("Thread.__init__() not called")
  1024. if daemonic and not _daemon_threads_allowed():
  1025. raise RuntimeError('daemon threads are disabled in this interpreter')
  1026. if self._started.is_set():
  1027. raise RuntimeError("cannot set daemon status of active thread")
  1028. self._daemonic = daemonic
  1029. def isDaemon(self):
  1030. """Return whether this thread is a daemon.
  1031. This method is deprecated, use the daemon attribute instead.
  1032. """
  1033. import warnings
  1034. warnings.warn('isDaemon() is deprecated, get the daemon attribute instead',
  1035. DeprecationWarning, stacklevel=2)
  1036. return self.daemon
  1037. def setDaemon(self, daemonic):
  1038. """Set whether this thread is a daemon.
  1039. This method is deprecated, use the .daemon property instead.
  1040. """
  1041. import warnings
  1042. warnings.warn('setDaemon() is deprecated, set the daemon attribute instead',
  1043. DeprecationWarning, stacklevel=2)
  1044. self.daemon = daemonic
  1045. def getName(self):
  1046. """Return a string used for identification purposes only.
  1047. This method is deprecated, use the name attribute instead.
  1048. """
  1049. import warnings
  1050. warnings.warn('getName() is deprecated, get the name attribute instead',
  1051. DeprecationWarning, stacklevel=2)
  1052. return self.name
  1053. def setName(self, name):
  1054. """Set the name string for this thread.
  1055. This method is deprecated, use the name attribute instead.
  1056. """
  1057. import warnings
  1058. warnings.warn('setName() is deprecated, set the name attribute instead',
  1059. DeprecationWarning, stacklevel=2)
  1060. self.name = name
  1061. try:
  1062. from _thread import (_excepthook as excepthook,
  1063. _ExceptHookArgs as ExceptHookArgs)
  1064. except ImportError:
  1065. # Simple Python implementation if _thread._excepthook() is not available
  1066. from traceback import print_exception as _print_exception
  1067. from collections import namedtuple
  1068. _ExceptHookArgs = namedtuple(
  1069. 'ExceptHookArgs',
  1070. 'exc_type exc_value exc_traceback thread')
  1071. def ExceptHookArgs(args):
  1072. return _ExceptHookArgs(*args)
  1073. def excepthook(args, /):
  1074. """
  1075. Handle uncaught Thread.run() exception.
  1076. """
  1077. if args.exc_type == SystemExit:
  1078. # silently ignore SystemExit
  1079. return
  1080. if _sys is not None and _sys.stderr is not None:
  1081. stderr = _sys.stderr
  1082. elif args.thread is not None:
  1083. stderr = args.thread._stderr
  1084. if stderr is None:
  1085. # do nothing if sys.stderr is None and sys.stderr was None
  1086. # when the thread was created
  1087. return
  1088. else:
  1089. # do nothing if sys.stderr is None and args.thread is None
  1090. return
  1091. if args.thread is not None:
  1092. name = args.thread.name
  1093. else:
  1094. name = get_ident()
  1095. print(f"Exception in thread {name}:",
  1096. file=stderr, flush=True)
  1097. _print_exception(args.exc_type, args.exc_value, args.exc_traceback,
  1098. file=stderr)
  1099. stderr.flush()
  1100. # Original value of threading.excepthook
  1101. __excepthook__ = excepthook
  1102. def _make_invoke_excepthook():
  1103. # Create a local namespace to ensure that variables remain alive
  1104. # when _invoke_excepthook() is called, even if it is called late during
  1105. # Python shutdown. It is mostly needed for daemon threads.
  1106. old_excepthook = excepthook
  1107. old_sys_excepthook = _sys.excepthook
  1108. if old_excepthook is None:
  1109. raise RuntimeError("threading.excepthook is None")
  1110. if old_sys_excepthook is None:
  1111. raise RuntimeError("sys.excepthook is None")
  1112. sys_exc_info = _sys.exc_info
  1113. local_print = print
  1114. local_sys = _sys
  1115. def invoke_excepthook(thread):
  1116. global excepthook
  1117. try:
  1118. hook = excepthook
  1119. if hook is None:
  1120. hook = old_excepthook
  1121. args = ExceptHookArgs([*sys_exc_info(), thread])
  1122. hook(args)
  1123. except Exception as exc:
  1124. exc.__suppress_context__ = True
  1125. del exc
  1126. if local_sys is not None and local_sys.stderr is not None:
  1127. stderr = local_sys.stderr
  1128. else:
  1129. stderr = thread._stderr
  1130. local_print("Exception in threading.excepthook:",
  1131. file=stderr, flush=True)
  1132. if local_sys is not None and local_sys.excepthook is not None:
  1133. sys_excepthook = local_sys.excepthook
  1134. else:
  1135. sys_excepthook = old_sys_excepthook
  1136. sys_excepthook(*sys_exc_info())
  1137. finally:
  1138. # Break reference cycle (exception stored in a variable)
  1139. args = None
  1140. return invoke_excepthook
  1141. # The timer class was contributed by Itamar Shtull-Trauring
  1142. class Timer(Thread):
  1143. """Call a function after a specified number of seconds:
  1144. t = Timer(30.0, f, args=None, kwargs=None)
  1145. t.start()
  1146. t.cancel() # stop the timer's action if it's still waiting
  1147. """
  1148. def __init__(self, interval, function, args=None, kwargs=None):
  1149. Thread.__init__(self)
  1150. self.interval = interval
  1151. self.function = function
  1152. self.args = args if args is not None else []
  1153. self.kwargs = kwargs if kwargs is not None else {}
  1154. self.finished = Event()
  1155. def cancel(self):
  1156. """Stop the timer if it hasn't finished yet."""
  1157. self.finished.set()
  1158. def run(self):
  1159. self.finished.wait(self.interval)
  1160. if not self.finished.is_set():
  1161. self.function(*self.args, **self.kwargs)
  1162. self.finished.set()
  1163. # Special thread class to represent the main thread
  1164. class _MainThread(Thread):
  1165. def __init__(self):
  1166. Thread.__init__(self, name="MainThread", daemon=False)
  1167. self._set_tstate_lock()
  1168. self._started.set()
  1169. self._set_ident()
  1170. if _HAVE_THREAD_NATIVE_ID:
  1171. self._set_native_id()
  1172. with _active_limbo_lock:
  1173. _active[self._ident] = self
  1174. # Dummy thread class to represent threads not started here.
  1175. # These aren't garbage collected when they die, nor can they be waited for.
  1176. # If they invoke anything in threading.py that calls current_thread(), they
  1177. # leave an entry in the _active dict forever after.
  1178. # Their purpose is to return *something* from current_thread().
  1179. # They are marked as daemon threads so we won't wait for them
  1180. # when we exit (conform previous semantics).
  1181. class _DummyThread(Thread):
  1182. def __init__(self):
  1183. Thread.__init__(self, name=_newname("Dummy-%d"),
  1184. daemon=_daemon_threads_allowed())
  1185. self._started.set()
  1186. self._set_ident()
  1187. if _HAVE_THREAD_NATIVE_ID:
  1188. self._set_native_id()
  1189. with _active_limbo_lock:
  1190. _active[self._ident] = self
  1191. def _stop(self):
  1192. pass
  1193. def is_alive(self):
  1194. assert not self._is_stopped and self._started.is_set()
  1195. return True
  1196. def join(self, timeout=None):
  1197. assert False, "cannot join a dummy thread"
  1198. # Global API functions
  1199. def current_thread():
  1200. """Return the current Thread object, corresponding to the caller's thread of control.
  1201. If the caller's thread of control was not created through the threading
  1202. module, a dummy thread object with limited functionality is returned.
  1203. """
  1204. try:
  1205. return _active[get_ident()]
  1206. except KeyError:
  1207. return _DummyThread()
  1208. def currentThread():
  1209. """Return the current Thread object, corresponding to the caller's thread of control.
  1210. This function is deprecated, use current_thread() instead.
  1211. """
  1212. import warnings
  1213. warnings.warn('currentThread() is deprecated, use current_thread() instead',
  1214. DeprecationWarning, stacklevel=2)
  1215. return current_thread()
  1216. def active_count():
  1217. """Return the number of Thread objects currently alive.
  1218. The returned count is equal to the length of the list returned by
  1219. enumerate().
  1220. """
  1221. # NOTE: if the logic in here ever changes, update Modules/posixmodule.c
  1222. # warn_about_fork_with_threads() to match.
  1223. with _active_limbo_lock:
  1224. return len(_active) + len(_limbo)
  1225. def activeCount():
  1226. """Return the number of Thread objects currently alive.
  1227. This function is deprecated, use active_count() instead.
  1228. """
  1229. import warnings
  1230. warnings.warn('activeCount() is deprecated, use active_count() instead',
  1231. DeprecationWarning, stacklevel=2)
  1232. return active_count()
  1233. def _enumerate():
  1234. # Same as enumerate(), but without the lock. Internal use only.
  1235. return list(_active.values()) + list(_limbo.values())
  1236. def enumerate():
  1237. """Return a list of all Thread objects currently alive.
  1238. The list includes daemonic threads, dummy thread objects created by
  1239. current_thread(), and the main thread. It excludes terminated threads and
  1240. threads that have not yet been started.
  1241. """
  1242. with _active_limbo_lock:
  1243. return list(_active.values()) + list(_limbo.values())
  1244. _threading_atexits = []
  1245. _SHUTTING_DOWN = False
  1246. def _register_atexit(func, *arg, **kwargs):
  1247. """CPython internal: register *func* to be called before joining threads.
  1248. The registered *func* is called with its arguments just before all
  1249. non-daemon threads are joined in `_shutdown()`. It provides a similar
  1250. purpose to `atexit.register()`, but its functions are called prior to
  1251. threading shutdown instead of interpreter shutdown.
  1252. For similarity to atexit, the registered functions are called in reverse.
  1253. """
  1254. if _SHUTTING_DOWN:
  1255. raise RuntimeError("can't register atexit after shutdown")
  1256. call = functools.partial(func, *arg, **kwargs)
  1257. _threading_atexits.append(call)
  1258. from _thread import stack_size
  1259. # Create the main thread object,
  1260. # and make it available for the interpreter
  1261. # (Py_Main) as threading._shutdown.
  1262. _main_thread = _MainThread()
  1263. def _shutdown():
  1264. """
  1265. Wait until the Python thread state of all non-daemon threads get deleted.
  1266. """
  1267. # Obscure: other threads may be waiting to join _main_thread. That's
  1268. # dubious, but some code does it. We can't wait for C code to release
  1269. # the main thread's tstate_lock - that won't happen until the interpreter
  1270. # is nearly dead. So we release it here. Note that just calling _stop()
  1271. # isn't enough: other threads may already be waiting on _tstate_lock.
  1272. if _main_thread._is_stopped and _is_main_interpreter():
  1273. # _shutdown() was already called
  1274. return
  1275. global _SHUTTING_DOWN
  1276. _SHUTTING_DOWN = True
  1277. # Call registered threading atexit functions before threads are joined.
  1278. # Order is reversed, similar to atexit.
  1279. for atexit_call in reversed(_threading_atexits):
  1280. atexit_call()
  1281. # Main thread
  1282. if _main_thread.ident == get_ident():
  1283. tlock = _main_thread._tstate_lock
  1284. # The main thread isn't finished yet, so its thread state lock can't
  1285. # have been released.
  1286. assert tlock is not None
  1287. assert tlock.locked()
  1288. tlock.release()
  1289. _main_thread._stop()
  1290. else:
  1291. # bpo-1596321: _shutdown() must be called in the main thread.
  1292. # If the threading module was not imported by the main thread,
  1293. # _main_thread is the thread which imported the threading module.
  1294. # In this case, ignore _main_thread, similar behavior than for threads
  1295. # spawned by C libraries or using _thread.start_new_thread().
  1296. pass
  1297. # Join all non-deamon threads
  1298. while True:
  1299. with _shutdown_locks_lock:
  1300. locks = list(_shutdown_locks)
  1301. _shutdown_locks.clear()
  1302. if not locks:
  1303. break
  1304. for lock in locks:
  1305. # mimic Thread.join()
  1306. lock.acquire()
  1307. lock.release()
  1308. # new threads can be spawned while we were waiting for the other
  1309. # threads to complete
  1310. def main_thread():
  1311. """Return the main thread object.
  1312. In normal conditions, the main thread is the thread from which the
  1313. Python interpreter was started.
  1314. """
  1315. # XXX Figure this out for subinterpreters. (See gh-75698.)
  1316. return _main_thread
  1317. # get thread-local implementation, either from the thread
  1318. # module, or from the python fallback
  1319. try:
  1320. from _thread import _local as local
  1321. except ImportError:
  1322. from _threading_local import local
  1323. def _after_fork():
  1324. """
  1325. Cleanup threading module state that should not exist after a fork.
  1326. """
  1327. # Reset _active_limbo_lock, in case we forked while the lock was held
  1328. # by another (non-forked) thread. http://bugs.python.org/issue874900
  1329. global _active_limbo_lock, _main_thread
  1330. global _shutdown_locks_lock, _shutdown_locks
  1331. _active_limbo_lock = RLock()
  1332. # fork() only copied the current thread; clear references to others.
  1333. new_active = {}
  1334. try:
  1335. current = _active[get_ident()]
  1336. except KeyError:
  1337. # fork() was called in a thread which was not spawned
  1338. # by threading.Thread. For example, a thread spawned
  1339. # by thread.start_new_thread().
  1340. current = _MainThread()
  1341. _main_thread = current
  1342. # reset _shutdown() locks: threads re-register their _tstate_lock below
  1343. _shutdown_locks_lock = _allocate_lock()
  1344. _shutdown_locks = set()
  1345. with _active_limbo_lock:
  1346. # Dangling thread instances must still have their locks reset,
  1347. # because someone may join() them.
  1348. threads = set(_enumerate())
  1349. threads.update(_dangling)
  1350. for thread in threads:
  1351. # Any lock/condition variable may be currently locked or in an
  1352. # invalid state, so we reinitialize them.
  1353. if thread is current:
  1354. # There is only one active thread. We reset the ident to
  1355. # its new value since it can have changed.
  1356. thread._reset_internal_locks(True)
  1357. ident = get_ident()
  1358. if isinstance(thread, _DummyThread):
  1359. thread.__class__ = _MainThread
  1360. thread._name = 'MainThread'
  1361. thread._daemonic = False
  1362. thread._set_tstate_lock()
  1363. thread._ident = ident
  1364. new_active[ident] = thread
  1365. else:
  1366. # All the others are already stopped.
  1367. thread._reset_internal_locks(False)
  1368. thread._stop()
  1369. _limbo.clear()
  1370. _active.clear()
  1371. _active.update(new_active)
  1372. assert len(_active) == 1
  1373. if hasattr(_os, "register_at_fork"):
  1374. _os.register_at_fork(after_in_child=_after_fork)