/Lib/threading.py

http://unladen-swallow.googlecode.com/ · Python · 1001 lines · 837 code · 73 blank · 91 comment · 92 complexity · 94436e7630f60b5d787cfde63ada9707 MD5 · raw file

  1. """Thread module emulating a subset of Java's threading model."""
  2. import sys as _sys
  3. try:
  4. import thread
  5. except ImportError:
  6. del _sys.modules[__name__]
  7. raise
  8. import warnings
  9. from functools import wraps
  10. from time import time as _time, sleep as _sleep
  11. from traceback import format_exc as _format_exc
  12. from collections import deque
  13. # Note regarding PEP 8 compliant aliases
  14. # This threading model was originally inspired by Java, and inherited
  15. # the convention of camelCase function and method names from that
  16. # language. While those names are not in any imminent danger of being
  17. # deprecated, starting with Python 2.6, the module now provides a
  18. # PEP 8 compliant alias for any such method name.
  19. # Using the new PEP 8 compliant names also facilitates substitution
  20. # with the multiprocessing module, which doesn't provide the old
  21. # Java inspired names.
  22. # Rename some stuff so "from threading import *" is safe
  23. __all__ = ['activeCount', 'active_count', 'Condition', 'currentThread',
  24. 'current_thread', 'enumerate', 'Event',
  25. 'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
  26. 'Timer', 'setprofile', 'settrace', 'local', 'stack_size']
  27. _start_new_thread = thread.start_new_thread
  28. _allocate_lock = thread.allocate_lock
  29. _get_ident = thread.get_ident
  30. ThreadError = thread.error
  31. del thread
  32. # sys.exc_clear is used to work around the fact that except blocks
  33. # don't fully clear the exception until 3.0.
  34. warnings.filterwarnings('ignore', category=DeprecationWarning,
  35. module='threading', message='sys.exc_clear')
  36. # Debug support (adapted from ihooks.py).
  37. # All the major classes here derive from _Verbose. We force that to
  38. # be a new-style class so that all the major classes here are new-style.
  39. # This helps debugging (type(instance) is more revealing for instances
  40. # of new-style classes).
  41. _VERBOSE = False
  42. if __debug__:
  43. class _Verbose(object):
  44. def __init__(self, verbose=None):
  45. if verbose is None:
  46. verbose = _VERBOSE
  47. self.__verbose = verbose
  48. def _note(self, format, *args):
  49. if self.__verbose:
  50. format = format % args
  51. format = "%s: %s\n" % (
  52. current_thread().name, format)
  53. _sys.stderr.write(format)
  54. else:
  55. # Disable this when using "python -O"
  56. class _Verbose(object):
  57. def __init__(self, verbose=None):
  58. pass
  59. def _note(self, *args):
  60. pass
  61. # Support for profile and trace hooks
  62. _profile_hook = None
  63. _trace_hook = None
  64. def setprofile(func):
  65. global _profile_hook
  66. _profile_hook = func
  67. def settrace(func):
  68. global _trace_hook
  69. _trace_hook = func
  70. # Synchronization classes
  71. Lock = _allocate_lock
  72. def RLock(*args, **kwargs):
  73. return _RLock(*args, **kwargs)
  74. class _RLock(_Verbose):
  75. def __init__(self, verbose=None):
  76. _Verbose.__init__(self, verbose)
  77. self.__block = _allocate_lock()
  78. self.__owner = None
  79. self.__count = 0
  80. def __repr__(self):
  81. owner = self.__owner
  82. return "<%s(%s, %d)>" % (
  83. self.__class__.__name__,
  84. owner and owner.name,
  85. self.__count)
  86. def acquire(self, blocking=1):
  87. me = current_thread()
  88. if self.__owner is me:
  89. self.__count = self.__count + 1
  90. if __debug__:
  91. self._note("%s.acquire(%s): recursive success", self, blocking)
  92. return 1
  93. rc = self.__block.acquire(blocking)
  94. if rc:
  95. self.__owner = me
  96. self.__count = 1
  97. if __debug__:
  98. self._note("%s.acquire(%s): initial success", self, blocking)
  99. else:
  100. if __debug__:
  101. self._note("%s.acquire(%s): failure", self, blocking)
  102. return rc
  103. __enter__ = acquire
  104. def release(self):
  105. if self.__owner is not current_thread():
  106. raise RuntimeError("cannot release un-aquired lock")
  107. self.__count = count = self.__count - 1
  108. if not count:
  109. self.__owner = None
  110. self.__block.release()
  111. if __debug__:
  112. self._note("%s.release(): final release", self)
  113. else:
  114. if __debug__:
  115. self._note("%s.release(): non-final release", self)
  116. def __exit__(self, t, v, tb):
  117. self.release()
  118. # Internal methods used by condition variables
  119. def _acquire_restore(self, count_owner):
  120. count, owner = count_owner
  121. self.__block.acquire()
  122. self.__count = count
  123. self.__owner = owner
  124. if __debug__:
  125. self._note("%s._acquire_restore()", self)
  126. def _release_save(self):
  127. if __debug__:
  128. self._note("%s._release_save()", self)
  129. count = self.__count
  130. self.__count = 0
  131. owner = self.__owner
  132. self.__owner = None
  133. self.__block.release()
  134. return (count, owner)
  135. def _is_owned(self):
  136. return self.__owner is current_thread()
  137. def Condition(*args, **kwargs):
  138. return _Condition(*args, **kwargs)
  139. class _Condition(_Verbose):
  140. def __init__(self, lock=None, verbose=None):
  141. _Verbose.__init__(self, verbose)
  142. if lock is None:
  143. lock = RLock()
  144. self.__lock = lock
  145. # Export the lock's acquire() and release() methods
  146. self.acquire = lock.acquire
  147. self.release = lock.release
  148. # If the lock defines _release_save() and/or _acquire_restore(),
  149. # these override the default implementations (which just call
  150. # release() and acquire() on the lock). Ditto for _is_owned().
  151. try:
  152. self._release_save = lock._release_save
  153. except AttributeError:
  154. pass
  155. try:
  156. self._acquire_restore = lock._acquire_restore
  157. except AttributeError:
  158. pass
  159. try:
  160. self._is_owned = lock._is_owned
  161. except AttributeError:
  162. pass
  163. self.__waiters = []
  164. def __enter__(self):
  165. return self.__lock.__enter__()
  166. def __exit__(self, *args):
  167. return self.__lock.__exit__(*args)
  168. def __repr__(self):
  169. return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters))
  170. def _release_save(self):
  171. self.__lock.release() # No state to save
  172. def _acquire_restore(self, x):
  173. self.__lock.acquire() # Ignore saved state
  174. def _is_owned(self):
  175. # Return True if lock is owned by current_thread.
  176. # This method is called only if __lock doesn't have _is_owned().
  177. if self.__lock.acquire(0):
  178. self.__lock.release()
  179. return False
  180. else:
  181. return True
  182. def _reset_after_fork(self, lock=None):
  183. """Throw away the old lock and replace it with this one."""
  184. if lock is None:
  185. lock = Lock()
  186. self.__lock = lock
  187. # Reset these exported bound methods. If we don't do this, these
  188. # bound methods will still refer to the old lock.
  189. self.acquire = lock.acquire
  190. self.release = lock.release
  191. # Any thread that may have been waiting on this condition no longer
  192. # exists after the fork. Even though we know the waiter locks must be
  193. # in the acquired state, attempting to release these locks is dangerous
  194. # on some platforms, such as OS X.
  195. self.__waiters = []
  196. def wait(self, timeout=None):
  197. if not self._is_owned():
  198. raise RuntimeError("cannot wait on un-aquired lock")
  199. waiter = _allocate_lock()
  200. waiter.acquire()
  201. self.__waiters.append(waiter)
  202. saved_state = self._release_save()
  203. try: # restore state no matter what (e.g., KeyboardInterrupt)
  204. if timeout is None:
  205. waiter.acquire()
  206. if __debug__:
  207. self._note("%s.wait(): got it", self)
  208. else:
  209. # Balancing act: We can't afford a pure busy loop, so we
  210. # have to sleep; but if we sleep the whole timeout time,
  211. # we'll be unresponsive. The scheme here sleeps very
  212. # little at first, longer as time goes on, but never longer
  213. # than 20 times per second (or the timeout time remaining).
  214. endtime = _time() + timeout
  215. delay = 0.0005 # 500 us -> initial delay of 1 ms
  216. while True:
  217. gotit = waiter.acquire(0)
  218. if gotit:
  219. break
  220. remaining = endtime - _time()
  221. if remaining <= 0:
  222. break
  223. delay = min(delay * 2, remaining, .05)
  224. _sleep(delay)
  225. if not gotit:
  226. if __debug__:
  227. self._note("%s.wait(%s): timed out", self, timeout)
  228. try:
  229. self.__waiters.remove(waiter)
  230. except ValueError:
  231. pass
  232. else:
  233. if __debug__:
  234. self._note("%s.wait(%s): got it", self, timeout)
  235. finally:
  236. self._acquire_restore(saved_state)
  237. def notify(self, n=1):
  238. if not self._is_owned():
  239. raise RuntimeError("cannot notify on un-aquired lock")
  240. __waiters = self.__waiters
  241. waiters = __waiters[:n]
  242. if not waiters:
  243. if __debug__:
  244. self._note("%s.notify(): no waiters", self)
  245. return
  246. self._note("%s.notify(): notifying %d waiter%s", self, n,
  247. n!=1 and "s" or "")
  248. for waiter in waiters:
  249. waiter.release()
  250. try:
  251. __waiters.remove(waiter)
  252. except ValueError:
  253. pass
  254. def notifyAll(self):
  255. self.notify(len(self.__waiters))
  256. notify_all = notifyAll
  257. def Semaphore(*args, **kwargs):
  258. return _Semaphore(*args, **kwargs)
  259. class _Semaphore(_Verbose):
  260. # After Tim Peters' semaphore class, but not quite the same (no maximum)
  261. def __init__(self, value=1, verbose=None):
  262. if value < 0:
  263. raise ValueError("semaphore initial value must be >= 0")
  264. _Verbose.__init__(self, verbose)
  265. self.__cond = Condition(Lock())
  266. self.__value = value
  267. def acquire(self, blocking=1):
  268. rc = False
  269. self.__cond.acquire()
  270. while self.__value == 0:
  271. if not blocking:
  272. break
  273. if __debug__:
  274. self._note("%s.acquire(%s): blocked waiting, value=%s",
  275. self, blocking, self.__value)
  276. self.__cond.wait()
  277. else:
  278. self.__value = self.__value - 1
  279. if __debug__:
  280. self._note("%s.acquire: success, value=%s",
  281. self, self.__value)
  282. rc = True
  283. self.__cond.release()
  284. return rc
  285. __enter__ = acquire
  286. def release(self):
  287. self.__cond.acquire()
  288. self.__value = self.__value + 1
  289. if __debug__:
  290. self._note("%s.release: success, value=%s",
  291. self, self.__value)
  292. self.__cond.notify()
  293. self.__cond.release()
  294. def __exit__(self, t, v, tb):
  295. self.release()
  296. def BoundedSemaphore(*args, **kwargs):
  297. return _BoundedSemaphore(*args, **kwargs)
  298. class _BoundedSemaphore(_Semaphore):
  299. """Semaphore that checks that # releases is <= # acquires"""
  300. def __init__(self, value=1, verbose=None):
  301. _Semaphore.__init__(self, value, verbose)
  302. self._initial_value = value
  303. def release(self):
  304. if self._Semaphore__value >= self._initial_value:
  305. raise ValueError, "Semaphore released too many times"
  306. return _Semaphore.release(self)
  307. def Event(*args, **kwargs):
  308. return _Event(*args, **kwargs)
  309. class _Event(_Verbose):
  310. # After Tim Peters' event class (without is_posted())
  311. def __init__(self, verbose=None):
  312. _Verbose.__init__(self, verbose)
  313. self.__cond = Condition(Lock())
  314. self.__flag = False
  315. def isSet(self):
  316. return self.__flag
  317. is_set = isSet
  318. def set(self):
  319. self.__cond.acquire()
  320. try:
  321. self.__flag = True
  322. self.__cond.notify_all()
  323. finally:
  324. self.__cond.release()
  325. def clear(self):
  326. self.__cond.acquire()
  327. try:
  328. self.__flag = False
  329. finally:
  330. self.__cond.release()
  331. def wait(self, timeout=None):
  332. self.__cond.acquire()
  333. try:
  334. if not self.__flag:
  335. self.__cond.wait(timeout)
  336. finally:
  337. self.__cond.release()
  338. # Helper to generate new thread names
  339. _counter = 0
  340. def _newname(template="Thread-%d"):
  341. global _counter
  342. _counter = _counter + 1
  343. return template % _counter
  344. # Active thread administration
  345. _active_limbo_lock = _allocate_lock()
  346. _active = {} # maps thread id to Thread object
  347. _limbo = {}
  348. # Main class for threads
  349. class Thread(_Verbose):
  350. __initialized = False
  351. # Need to store a reference to sys.exc_info for printing
  352. # out exceptions when a thread tries to use a global var. during interp.
  353. # shutdown and thus raises an exception about trying to perform some
  354. # operation on/with a NoneType
  355. __exc_info = _sys.exc_info
  356. # Keep sys.exc_clear too to clear the exception just before
  357. # allowing .join() to return.
  358. __exc_clear = _sys.exc_clear
  359. def __init__(self, group=None, target=None, name=None,
  360. args=(), kwargs=None, verbose=None):
  361. assert group is None, "group argument must be None for now"
  362. _Verbose.__init__(self, verbose)
  363. if kwargs is None:
  364. kwargs = {}
  365. self.__target = target
  366. self.__name = str(name or _newname())
  367. self.__args = args
  368. self.__kwargs = kwargs
  369. self.__daemonic = self._set_daemon()
  370. self.__ident = None
  371. self.__started = Event()
  372. self.__stopped = False
  373. self.__block = Condition(Lock())
  374. self.__initialized = True
  375. # sys.stderr is not stored in the class like
  376. # sys.exc_info since it can be changed between instances
  377. self.__stderr = _sys.stderr
  378. def _set_daemon(self):
  379. # Overridden in _MainThread and _DummyThread
  380. return current_thread().daemon
  381. def __repr__(self):
  382. assert self.__initialized, "Thread.__init__() was not called"
  383. status = "initial"
  384. if self.__started.is_set():
  385. status = "started"
  386. if self.__stopped:
  387. status = "stopped"
  388. if self.__daemonic:
  389. status += " daemon"
  390. if self.__ident is not None:
  391. status += " %s" % self.__ident
  392. return "<%s(%s, %s)>" % (self.__class__.__name__, self.__name, status)
  393. def start(self):
  394. if not self.__initialized:
  395. raise RuntimeError("thread.__init__() not called")
  396. if self.__started.is_set():
  397. raise RuntimeError("thread already started")
  398. if __debug__:
  399. self._note("%s.start(): starting thread", self)
  400. _active_limbo_lock.acquire()
  401. _limbo[self] = self
  402. _active_limbo_lock.release()
  403. _start_new_thread(self.__bootstrap, ())
  404. self.__started.wait()
  405. def run(self):
  406. try:
  407. if self.__target:
  408. self.__target(*self.__args, **self.__kwargs)
  409. finally:
  410. # Avoid a refcycle if the thread is running a function with
  411. # an argument that has a member that points to the thread.
  412. del self.__target, self.__args, self.__kwargs
  413. def __bootstrap(self):
  414. # Wrapper around the real bootstrap code that ignores
  415. # exceptions during interpreter cleanup. Those typically
  416. # happen when a daemon thread wakes up at an unfortunate
  417. # moment, finds the world around it destroyed, and raises some
  418. # random exception *** while trying to report the exception in
  419. # __bootstrap_inner() below ***. Those random exceptions
  420. # don't help anybody, and they confuse users, so we suppress
  421. # them. We suppress them only when it appears that the world
  422. # indeed has already been destroyed, so that exceptions in
  423. # __bootstrap_inner() during normal business hours are properly
  424. # reported. Also, we only suppress them for daemonic threads;
  425. # if a non-daemonic encounters this, something else is wrong.
  426. try:
  427. self.__bootstrap_inner()
  428. except:
  429. if self.__daemonic and _sys is None:
  430. return
  431. raise
  432. def _set_ident(self):
  433. self.__ident = _get_ident()
  434. def __bootstrap_inner(self):
  435. try:
  436. self._set_ident()
  437. self.__started.set()
  438. _active_limbo_lock.acquire()
  439. _active[self.__ident] = self
  440. del _limbo[self]
  441. _active_limbo_lock.release()
  442. if __debug__:
  443. self._note("%s.__bootstrap(): thread started", self)
  444. if _trace_hook:
  445. self._note("%s.__bootstrap(): registering trace hook", self)
  446. _sys.settrace(_trace_hook)
  447. if _profile_hook:
  448. self._note("%s.__bootstrap(): registering profile hook", self)
  449. _sys.setprofile(_profile_hook)
  450. try:
  451. self.run()
  452. except SystemExit:
  453. if __debug__:
  454. self._note("%s.__bootstrap(): raised SystemExit", self)
  455. except:
  456. if __debug__:
  457. self._note("%s.__bootstrap(): unhandled exception", self)
  458. # If sys.stderr is no more (most likely from interpreter
  459. # shutdown) use self.__stderr. Otherwise still use sys (as in
  460. # _sys) in case sys.stderr was redefined since the creation of
  461. # self.
  462. if _sys:
  463. _sys.stderr.write("Exception in thread %s:\n%s\n" %
  464. (self.name, _format_exc()))
  465. else:
  466. # Do the best job possible w/o a huge amt. of code to
  467. # approximate a traceback (code ideas from
  468. # Lib/traceback.py)
  469. exc_type, exc_value, exc_tb = self.__exc_info()
  470. try:
  471. print>>self.__stderr, (
  472. "Exception in thread " + self.name +
  473. " (most likely raised during interpreter shutdown):")
  474. print>>self.__stderr, (
  475. "Traceback (most recent call last):")
  476. while exc_tb:
  477. print>>self.__stderr, (
  478. ' File "%s", line %s, in %s' %
  479. (exc_tb.tb_frame.f_code.co_filename,
  480. exc_tb.tb_lineno,
  481. exc_tb.tb_frame.f_code.co_name))
  482. exc_tb = exc_tb.tb_next
  483. print>>self.__stderr, ("%s: %s" % (exc_type, exc_value))
  484. # Make sure that exc_tb gets deleted since it is a memory
  485. # hog; deleting everything else is just for thoroughness
  486. finally:
  487. del exc_type, exc_value, exc_tb
  488. else:
  489. if __debug__:
  490. self._note("%s.__bootstrap(): normal return", self)
  491. finally:
  492. # Prevent a race in
  493. # test_threading.test_no_refcycle_through_target when
  494. # the exception keeps the target alive past when we
  495. # assert that it's dead.
  496. self.__exc_clear()
  497. finally:
  498. with _active_limbo_lock:
  499. self.__stop()
  500. try:
  501. # We don't call self.__delete() because it also
  502. # grabs _active_limbo_lock.
  503. del _active[_get_ident()]
  504. except:
  505. pass
  506. def __stop(self):
  507. self.__block.acquire()
  508. self.__stopped = True
  509. self.__block.notify_all()
  510. self.__block.release()
  511. def __delete(self):
  512. "Remove current thread from the dict of currently running threads."
  513. # Notes about running with dummy_thread:
  514. #
  515. # Must take care to not raise an exception if dummy_thread is being
  516. # used (and thus this module is being used as an instance of
  517. # dummy_threading). dummy_thread.get_ident() always returns -1 since
  518. # there is only one thread if dummy_thread is being used. Thus
  519. # len(_active) is always <= 1 here, and any Thread instance created
  520. # overwrites the (if any) thread currently registered in _active.
  521. #
  522. # An instance of _MainThread is always created by 'threading'. This
  523. # gets overwritten the instant an instance of Thread is created; both
  524. # threads return -1 from dummy_thread.get_ident() and thus have the
  525. # same key in the dict. So when the _MainThread instance created by
  526. # 'threading' tries to clean itself up when atexit calls this method
  527. # it gets a KeyError if another Thread instance was created.
  528. #
  529. # This all means that KeyError from trying to delete something from
  530. # _active if dummy_threading is being used is a red herring. But
  531. # since it isn't if dummy_threading is *not* being used then don't
  532. # hide the exception.
  533. try:
  534. with _active_limbo_lock:
  535. del _active[_get_ident()]
  536. # There must not be any python code between the previous line
  537. # and after the lock is released. Otherwise a tracing function
  538. # could try to acquire the lock again in the same thread, (in
  539. # current_thread()), and would block.
  540. except KeyError:
  541. if 'dummy_threading' not in _sys.modules:
  542. raise
  543. def join(self, timeout=None):
  544. if not self.__initialized:
  545. raise RuntimeError("Thread.__init__() not called")
  546. if not self.__started.is_set():
  547. raise RuntimeError("cannot join thread before it is started")
  548. if self is current_thread():
  549. raise RuntimeError("cannot join current thread")
  550. if __debug__:
  551. if not self.__stopped:
  552. self._note("%s.join(): waiting until thread stops", self)
  553. self.__block.acquire()
  554. try:
  555. if timeout is None:
  556. while not self.__stopped:
  557. self.__block.wait()
  558. if __debug__:
  559. self._note("%s.join(): thread stopped", self)
  560. else:
  561. deadline = _time() + timeout
  562. while not self.__stopped:
  563. delay = deadline - _time()
  564. if delay <= 0:
  565. if __debug__:
  566. self._note("%s.join(): timed out", self)
  567. break
  568. self.__block.wait(delay)
  569. else:
  570. if __debug__:
  571. self._note("%s.join(): thread stopped", self)
  572. finally:
  573. self.__block.release()
  574. @property
  575. def name(self):
  576. assert self.__initialized, "Thread.__init__() not called"
  577. return self.__name
  578. @name.setter
  579. def name(self, name):
  580. assert self.__initialized, "Thread.__init__() not called"
  581. self.__name = str(name)
  582. @property
  583. def ident(self):
  584. assert self.__initialized, "Thread.__init__() not called"
  585. return self.__ident
  586. def isAlive(self):
  587. assert self.__initialized, "Thread.__init__() not called"
  588. return self.__started.is_set() and not self.__stopped
  589. is_alive = isAlive
  590. @property
  591. def daemon(self):
  592. assert self.__initialized, "Thread.__init__() not called"
  593. return self.__daemonic
  594. @daemon.setter
  595. def daemon(self, daemonic):
  596. if not self.__initialized:
  597. raise RuntimeError("Thread.__init__() not called")
  598. if self.__started.is_set():
  599. raise RuntimeError("cannot set daemon status of active thread");
  600. self.__daemonic = daemonic
  601. def isDaemon(self):
  602. return self.daemon
  603. def setDaemon(self, daemonic):
  604. self.daemon = daemonic
  605. def getName(self):
  606. return self.name
  607. def setName(self, name):
  608. self.name = name
  609. # The timer class was contributed by Itamar Shtull-Trauring
  610. def Timer(*args, **kwargs):
  611. return _Timer(*args, **kwargs)
  612. class _Timer(Thread):
  613. """Call a function after a specified number of seconds:
  614. t = Timer(30.0, f, args=[], kwargs={})
  615. t.start()
  616. t.cancel() # stop the timer's action if it's still waiting
  617. """
  618. def __init__(self, interval, function, args=[], kwargs={}):
  619. Thread.__init__(self)
  620. self.interval = interval
  621. self.function = function
  622. self.args = args
  623. self.kwargs = kwargs
  624. self.finished = Event()
  625. def cancel(self):
  626. """Stop the timer if it hasn't finished yet"""
  627. self.finished.set()
  628. def run(self):
  629. self.finished.wait(self.interval)
  630. if not self.finished.is_set():
  631. self.function(*self.args, **self.kwargs)
  632. self.finished.set()
  633. # Special thread class to represent the main thread
  634. # This is garbage collected through an exit handler
  635. class _MainThread(Thread):
  636. def __init__(self):
  637. Thread.__init__(self, name="MainThread")
  638. self._Thread__started.set()
  639. self._set_ident()
  640. _active_limbo_lock.acquire()
  641. _active[_get_ident()] = self
  642. _active_limbo_lock.release()
  643. def _set_daemon(self):
  644. return False
  645. def _pickSomeNonDaemonThread():
  646. for t in enumerate():
  647. if not t.daemon and t.is_alive():
  648. return t
  649. return None
  650. # Dummy thread class to represent threads not started here.
  651. # These aren't garbage collected when they die, nor can they be waited for.
  652. # If they invoke anything in threading.py that calls current_thread(), they
  653. # leave an entry in the _active dict forever after.
  654. # Their purpose is to return *something* from current_thread().
  655. # They are marked as daemon threads so we won't wait for them
  656. # when we exit (conform previous semantics).
  657. class _DummyThread(Thread):
  658. def __init__(self):
  659. Thread.__init__(self, name=_newname("Dummy-%d"))
  660. # Thread.__block consumes an OS-level locking primitive, which
  661. # can never be used by a _DummyThread. Since a _DummyThread
  662. # instance is immortal, that's bad, so release this resource.
  663. del self._Thread__block
  664. self._Thread__started.set()
  665. self._set_ident()
  666. _active_limbo_lock.acquire()
  667. _active[_get_ident()] = self
  668. _active_limbo_lock.release()
  669. def _set_daemon(self):
  670. return True
  671. def join(self, timeout=None):
  672. assert False, "cannot join a dummy thread"
  673. # Global API functions
  674. def currentThread():
  675. try:
  676. return _active[_get_ident()]
  677. except KeyError:
  678. ##print "current_thread(): no current thread for", _get_ident()
  679. return _DummyThread()
  680. current_thread = currentThread
  681. def activeCount():
  682. _active_limbo_lock.acquire()
  683. count = len(_active) + len(_limbo)
  684. _active_limbo_lock.release()
  685. return count
  686. active_count = activeCount
  687. def enumerate():
  688. _active_limbo_lock.acquire()
  689. active = _active.values() + _limbo.values()
  690. _active_limbo_lock.release()
  691. return active
  692. from thread import stack_size
  693. # Create the main thread object and record it's id. This puts it into the
  694. # active threads dictionary. This assumes that the all threads are created via
  695. # the threading module. If the program uses the thread module to spawn a
  696. # thread that imports threading, we'll choose the wrong main thread.
  697. _MainThread()
  698. main_thread_id = _get_ident()
  699. def _shutdown():
  700. """Shutdown handler that waits for all non-daemon threads to exit.
  701. This is called from the main thread during shut down.
  702. """
  703. # If this thread isn't in the _active dict, then it's because we forked
  704. # from a spanwed thread. In that case, because the main thread of the child
  705. # process was originally a spawned thread, it will have already removed
  706. # itself from the active dict by returning through __bootstrap_inner.
  707. # Therefore we don't stop and delete it.
  708. ident = _get_ident()
  709. assert ident == main_thread_id, \
  710. "Shutdown handler called from non-main thread!"
  711. main_thread = _active.get(ident, None)
  712. if main_thread:
  713. main_thread._Thread__stop()
  714. t = _pickSomeNonDaemonThread()
  715. if t and main_thread:
  716. if __debug__:
  717. main_thread._note("%s: waiting for other threads", main_thread)
  718. while t:
  719. t.join()
  720. t = _pickSomeNonDaemonThread()
  721. if __debug__ and main_thread:
  722. main_thread._note("%s: exiting", main_thread)
  723. if main_thread:
  724. main_thread._Thread__delete()
  725. # get thread-local implementation, either from the thread
  726. # module, or from the python fallback
  727. try:
  728. from thread import _local as local
  729. except ImportError:
  730. from _threading_local import local
  731. def _after_fork():
  732. # This function is called by Python/eval.cc:PyEval_ReInitThreads which
  733. # is called from PyOS_AfterFork. Here we cleanup threading module state
  734. # that should not exist after a fork.
  735. # Reset _active_limbo_lock, in case we forked while the lock was held
  736. # by another (non-forked) thread. http://bugs.python.org/issue874900
  737. global _active_limbo_lock
  738. _active_limbo_lock = _allocate_lock()
  739. # fork() only copied the current thread; clear references to others.
  740. new_active = {}
  741. current = current_thread()
  742. with _active_limbo_lock:
  743. for thread in _active.itervalues():
  744. if thread is current:
  745. # There is only one active thread. We reset the ident to
  746. # its new value since it can have changed.
  747. ident = _get_ident()
  748. thread._Thread__ident = ident
  749. # Any locks hanging off of the active thread may be in an
  750. # invalid state, so we reset them.
  751. thread._Thread__block._reset_after_fork()
  752. thread._Thread__started._Event__cond._reset_after_fork()
  753. # We also update this global variable so we can keep track of
  754. # the new main thread (whichever thread forked).
  755. global main_thread_id
  756. main_thread_id = ident
  757. new_active[ident] = thread
  758. else:
  759. # All the others are already stopped.
  760. # We don't call _Thread__stop() because it tries to acquire
  761. # thread._Thread__block which could also have been held while
  762. # we forked.
  763. thread._Thread__stopped = True
  764. _limbo.clear()
  765. _active.clear()
  766. _active.update(new_active)
  767. assert len(_active) == 1
  768. # Self-test code
  769. def _test():
  770. class BoundedQueue(_Verbose):
  771. def __init__(self, limit):
  772. _Verbose.__init__(self)
  773. self.mon = RLock()
  774. self.rc = Condition(self.mon)
  775. self.wc = Condition(self.mon)
  776. self.limit = limit
  777. self.queue = deque()
  778. def put(self, item):
  779. self.mon.acquire()
  780. while len(self.queue) >= self.limit:
  781. self._note("put(%s): queue full", item)
  782. self.wc.wait()
  783. self.queue.append(item)
  784. self._note("put(%s): appended, length now %d",
  785. item, len(self.queue))
  786. self.rc.notify()
  787. self.mon.release()
  788. def get(self):
  789. self.mon.acquire()
  790. while not self.queue:
  791. self._note("get(): queue empty")
  792. self.rc.wait()
  793. item = self.queue.popleft()
  794. self._note("get(): got %s, %d left", item, len(self.queue))
  795. self.wc.notify()
  796. self.mon.release()
  797. return item
  798. class ProducerThread(Thread):
  799. def __init__(self, queue, quota):
  800. Thread.__init__(self, name="Producer")
  801. self.queue = queue
  802. self.quota = quota
  803. def run(self):
  804. from random import random
  805. counter = 0
  806. while counter < self.quota:
  807. counter = counter + 1
  808. self.queue.put("%s.%d" % (self.name, counter))
  809. _sleep(random() * 0.00001)
  810. class ConsumerThread(Thread):
  811. def __init__(self, queue, count):
  812. Thread.__init__(self, name="Consumer")
  813. self.queue = queue
  814. self.count = count
  815. def run(self):
  816. while self.count > 0:
  817. item = self.queue.get()
  818. print item
  819. self.count = self.count - 1
  820. NP = 3
  821. QL = 4
  822. NI = 5
  823. Q = BoundedQueue(QL)
  824. P = []
  825. for i in range(NP):
  826. t = ProducerThread(Q, NI)
  827. t.name = ("Producer-%d" % (i+1))
  828. P.append(t)
  829. C = ConsumerThread(Q, NI*NP)
  830. for t in P:
  831. t.start()
  832. _sleep(0.000001)
  833. C.start()
  834. for t in P:
  835. t.join()
  836. C.join()
  837. if __name__ == '__main__':
  838. _test()