threading.py 53 KB

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