threading.py 48 KB

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