threading.py 58 KB

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