threading.py 51 KB

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