github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/third_party/stdlib/threading.py (about)

     1  """Thread module emulating a subset of Java's threading model."""
     2  
     3  import sys as _sys
     4  
     5  try:
     6      import thread
     7  except ImportError:
     8      del _sys.modules[__name__]
     9      raise
    10  
    11  import warnings
    12  
    13  from collections import deque as _deque
    14  from itertools import count as _count
    15  from time import time as _time, sleep as _sleep
    16  from traceback import format_exc as _format_exc
    17  
    18  # Note regarding PEP 8 compliant aliases
    19  #  This threading model was originally inspired by Java, and inherited
    20  # the convention of camelCase function and method names from that
    21  # language. While those names are not in any imminent danger of being
    22  # deprecated, starting with Python 2.6, the module now provides a
    23  # PEP 8 compliant alias for any such method name.
    24  # Using the new PEP 8 compliant names also facilitates substitution
    25  # with the multiprocessing module, which doesn't provide the old
    26  # Java inspired names.
    27  
    28  
    29  # Rename some stuff so "from threading import *" is safe
    30  __all__ = ['activeCount', 'active_count', 'Condition', 'currentThread',
    31             'current_thread', 'enumerate', 'Event',
    32             'Lock', 'RLock', 'Semaphore', 'BoundedSemaphore', 'Thread',
    33             'Timer', 'setprofile', 'settrace', 'local', 'stack_size']
    34  
    35  _start_new_thread = thread.start_new_thread
    36  _allocate_lock = thread.allocate_lock
    37  _get_ident = thread.get_ident
    38  ThreadError = thread.error
    39  del thread
    40  
    41  
    42  # sys.exc_clear is used to work around the fact that except blocks
    43  # don't fully clear the exception until 3.0.
    44  warnings.filterwarnings('ignore', category=DeprecationWarning,
    45                          module='threading', message='sys.exc_clear')
    46  
    47  # Debug support (adapted from ihooks.py).
    48  # All the major classes here derive from _Verbose.  We force that to
    49  # be a new-style class so that all the major classes here are new-style.
    50  # This helps debugging (type(instance) is more revealing for instances
    51  # of new-style classes).
    52  
    53  _VERBOSE = False
    54  
    55  if __debug__:
    56  
    57      class _Verbose(object):
    58  
    59          def __init__(self, verbose=None):
    60              if verbose is None:
    61                  verbose = _VERBOSE
    62              self.__verbose = verbose
    63  
    64          def _note(self, format, *args):
    65              if self.__verbose:
    66                  format = format % args
    67                  # Issue #4188: calling current_thread() can incur an infinite
    68                  # recursion if it has to create a DummyThread on the fly.
    69                  ident = _get_ident()
    70                  try:
    71                      name = _active[ident].name
    72                  except KeyError:
    73                      name = "<OS thread %d>" % ident
    74                  format = "%s: %s\n" % (name, format)
    75                  _sys.stderr.write(format)
    76  
    77  else:
    78      # Disable this when using "python -O"
    79      class _Verbose(object):
    80          def __init__(self, verbose=None):
    81              pass
    82          def _note(self, *args):
    83              pass
    84  
    85  # Support for profile and trace hooks
    86  
    87  _profile_hook = None
    88  _trace_hook = None
    89  
    90  def setprofile(func):
    91      """Set a profile function for all threads started from the threading module.
    92  
    93      The func will be passed to sys.setprofile() for each thread, before its
    94      run() method is called.
    95  
    96      """
    97      global _profile_hook
    98      _profile_hook = func
    99  
   100  def settrace(func):
   101      """Set a trace function for all threads started from the threading module.
   102  
   103      The func will be passed to sys.settrace() for each thread, before its run()
   104      method is called.
   105  
   106      """
   107      global _trace_hook
   108      _trace_hook = func
   109  
   110  # Synchronization classes
   111  
   112  Lock = _allocate_lock
   113  
   114  def RLock(*args, **kwargs):
   115      """Factory function that returns a new reentrant lock.
   116  
   117      A reentrant lock must be released by the thread that acquired it. Once a
   118      thread has acquired a reentrant lock, the same thread may acquire it again
   119      without blocking; the thread must release it once for each time it has
   120      acquired it.
   121  
   122      """
   123      return _RLock(*args, **kwargs)
   124  
   125  class _RLock(_Verbose):
   126      """A reentrant lock must be released by the thread that acquired it. Once a
   127         thread has acquired a reentrant lock, the same thread may acquire it
   128         again without blocking; the thread must release it once for each time it
   129         has acquired it.
   130      """
   131  
   132      def __init__(self, verbose=None):
   133          _Verbose.__init__(self, verbose)
   134          self.__block = _allocate_lock()
   135          self.__owner = None
   136          self.__count = 0
   137  
   138      def __repr__(self):
   139          owner = self.__owner
   140          try:
   141              owner = _active[owner].name
   142          except KeyError:
   143              pass
   144          return "<%s owner=%r count=%d>" % (
   145                  self.__class__.__name__, owner, self.__count)
   146  
   147      def acquire(self, blocking=1):
   148          """Acquire a lock, blocking or non-blocking.
   149  
   150          When invoked without arguments: if this thread already owns the lock,
   151          increment the recursion level by one, and return immediately. Otherwise,
   152          if another thread owns the lock, block until the lock is unlocked. Once
   153          the lock is unlocked (not owned by any thread), then grab ownership, set
   154          the recursion level to one, and return. If more than one thread is
   155          blocked waiting until the lock is unlocked, only one at a time will be
   156          able to grab ownership of the lock. There is no return value in this
   157          case.
   158  
   159          When invoked with the blocking argument set to true, do the same thing
   160          as when called without arguments, and return true.
   161  
   162          When invoked with the blocking argument set to false, do not block. If a
   163          call without an argument would block, return false immediately;
   164          otherwise, do the same thing as when called without arguments, and
   165          return true.
   166  
   167          """
   168          me = _get_ident()
   169          if self.__owner == me:
   170              self.__count = self.__count + 1
   171              if __debug__:
   172                  self._note("%s.acquire(%s): recursive success", self, blocking)
   173              return 1
   174          rc = self.__block.acquire(blocking)
   175          if rc:
   176              self.__owner = me
   177              self.__count = 1
   178              if __debug__:
   179                  self._note("%s.acquire(%s): initial success", self, blocking)
   180          else:
   181              if __debug__:
   182                  self._note("%s.acquire(%s): failure", self, blocking)
   183          return rc
   184  
   185      __enter__ = acquire
   186  
   187      def release(self):
   188          """Release a lock, decrementing the recursion level.
   189  
   190          If after the decrement it is zero, reset the lock to unlocked (not owned
   191          by any thread), and if any other threads are blocked waiting for the
   192          lock to become unlocked, allow exactly one of them to proceed. If after
   193          the decrement the recursion level is still nonzero, the lock remains
   194          locked and owned by the calling thread.
   195  
   196          Only call this method when the calling thread owns the lock. A
   197          RuntimeError is raised if this method is called when the lock is
   198          unlocked.
   199  
   200          There is no return value.
   201  
   202          """
   203          if self.__owner != _get_ident():
   204              raise RuntimeError("cannot release un-acquired lock")
   205          self.__count = count = self.__count - 1
   206          if not count:
   207              self.__owner = None
   208              self.__block.release()
   209              if __debug__:
   210                  self._note("%s.release(): final release", self)
   211          else:
   212              if __debug__:
   213                  self._note("%s.release(): non-final release", self)
   214  
   215      def __exit__(self, t, v, tb):
   216          self.release()
   217  
   218      # Internal methods used by condition variables
   219  
   220      def _acquire_restore(self, count_owner):
   221          count, owner = count_owner
   222          self.__block.acquire()
   223          self.__count = count
   224          self.__owner = owner
   225          if __debug__:
   226              self._note("%s._acquire_restore()", self)
   227  
   228      def _release_save(self):
   229          if __debug__:
   230              self._note("%s._release_save()", self)
   231          count = self.__count
   232          self.__count = 0
   233          owner = self.__owner
   234          self.__owner = None
   235          self.__block.release()
   236          return (count, owner)
   237  
   238      def _is_owned(self):
   239          return self.__owner == _get_ident()
   240  
   241  
   242  def Condition(*args, **kwargs):
   243      """Factory function that returns a new condition variable object.
   244  
   245      A condition variable allows one or more threads to wait until they are
   246      notified by another thread.
   247  
   248      If the lock argument is given and not None, it must be a Lock or RLock
   249      object, and it is used as the underlying lock. Otherwise, a new RLock object
   250      is created and used as the underlying lock.
   251  
   252      """
   253      return _Condition(*args, **kwargs)
   254  
   255  class _Condition(_Verbose):
   256      """Condition variables allow one or more threads to wait until they are
   257         notified by another thread.
   258      """
   259  
   260      def __init__(self, lock=None, verbose=None):
   261          _Verbose.__init__(self, verbose)
   262          if lock is None:
   263              lock = RLock()
   264          self.__lock = lock
   265          # Export the lock's acquire() and release() methods
   266          self.acquire = lock.acquire
   267          self.release = lock.release
   268          # If the lock defines _release_save() and/or _acquire_restore(),
   269          # these override the default implementations (which just call
   270          # release() and acquire() on the lock).  Ditto for _is_owned().
   271          try:
   272              self._release_save = lock._release_save
   273          except AttributeError:
   274              pass
   275          try:
   276              self._acquire_restore = lock._acquire_restore
   277          except AttributeError:
   278              pass
   279          try:
   280              self._is_owned = lock._is_owned
   281          except AttributeError:
   282              pass
   283          self.__waiters = []
   284  
   285      def __enter__(self):
   286          return self.__lock.__enter__()
   287  
   288      def __exit__(self, *args):
   289          return self.__lock.__exit__(*args)
   290  
   291      def __repr__(self):
   292          return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters))
   293  
   294      def _release_save(self):
   295          self.__lock.release()           # No state to save
   296  
   297      def _acquire_restore(self, x):
   298          self.__lock.acquire()           # Ignore saved state
   299  
   300      def _is_owned(self):
   301          # Return True if lock is owned by current_thread.
   302          # This method is called only if __lock doesn't have _is_owned().
   303          if self.__lock.acquire(0):
   304              self.__lock.release()
   305              return False
   306          else:
   307              return True
   308  
   309      def wait(self, timeout=None):
   310          """Wait until notified or until a timeout occurs.
   311  
   312          If the calling thread has not acquired the lock when this method is
   313          called, a RuntimeError is raised.
   314  
   315          This method releases the underlying lock, and then blocks until it is
   316          awakened by a notify() or notifyAll() call for the same condition
   317          variable in another thread, or until the optional timeout occurs. Once
   318          awakened or timed out, it re-acquires the lock and returns.
   319  
   320          When the timeout argument is present and not None, it should be a
   321          floating point number specifying a timeout for the operation in seconds
   322          (or fractions thereof).
   323  
   324          When the underlying lock is an RLock, it is not released using its
   325          release() method, since this may not actually unlock the lock when it
   326          was acquired multiple times recursively. Instead, an internal interface
   327          of the RLock class is used, which really unlocks it even when it has
   328          been recursively acquired several times. Another internal interface is
   329          then used to restore the recursion level when the lock is reacquired.
   330  
   331          """
   332          if not self._is_owned():
   333              raise RuntimeError("cannot wait on un-acquired lock")
   334          waiter = _allocate_lock()
   335          waiter.acquire()
   336          self.__waiters.append(waiter)
   337          saved_state = self._release_save()
   338          try:    # restore state no matter what (e.g., KeyboardInterrupt)
   339              if timeout is None:
   340                  waiter.acquire()
   341                  if __debug__:
   342                      self._note("%s.wait(): got it", self)
   343              else:
   344                  # Balancing act:  We can't afford a pure busy loop, so we
   345                  # have to sleep; but if we sleep the whole timeout time,
   346                  # we'll be unresponsive.  The scheme here sleeps very
   347                  # little at first, longer as time goes on, but never longer
   348                  # than 20 times per second (or the timeout time remaining).
   349                  endtime = _time() + timeout
   350                  delay = 0.0005 # 500 us -> initial delay of 1 ms
   351                  while True:
   352                      gotit = waiter.acquire(0)
   353                      if gotit:
   354                          break
   355                      remaining = endtime - _time()
   356                      if remaining <= 0:
   357                          break
   358                      delay = min(delay * 2, remaining, .05)
   359                      _sleep(delay)
   360                  if not gotit:
   361                      if __debug__:
   362                          self._note("%s.wait(%s): timed out", self, timeout)
   363                      try:
   364                          self.__waiters.remove(waiter)
   365                      except ValueError:
   366                          pass
   367                  else:
   368                      if __debug__:
   369                          self._note("%s.wait(%s): got it", self, timeout)
   370          finally:
   371              self._acquire_restore(saved_state)
   372  
   373      def notify(self, n=1):
   374          """Wake up one or more threads waiting on this condition, if any.
   375  
   376          If the calling thread has not acquired the lock when this method is
   377          called, a RuntimeError is raised.
   378  
   379          This method wakes up at most n of the threads waiting for the condition
   380          variable; it is a no-op if no threads are waiting.
   381  
   382          """
   383          if not self._is_owned():
   384              raise RuntimeError("cannot notify on un-acquired lock")
   385          __waiters = self.__waiters
   386          waiters = __waiters[:n]
   387          if not waiters:
   388              if __debug__:
   389                  self._note("%s.notify(): no waiters", self)
   390              return
   391          self._note("%s.notify(): notifying %d waiter%s", self, n,
   392                     n!=1 and "s" or "")
   393          for waiter in waiters:
   394              waiter.release()
   395              try:
   396                  __waiters.remove(waiter)
   397              except ValueError:
   398                  pass
   399  
   400      def notifyAll(self):
   401          """Wake up all threads waiting on this condition.
   402  
   403          If the calling thread has not acquired the lock when this method
   404          is called, a RuntimeError is raised.
   405  
   406          """
   407          self.notify(len(self.__waiters))
   408  
   409      notify_all = notifyAll
   410  
   411  
   412  def Semaphore(*args, **kwargs):
   413      """A factory function that returns a new semaphore.
   414  
   415      Semaphores manage a counter representing the number of release() calls minus
   416      the number of acquire() calls, plus an initial value. The acquire() method
   417      blocks if necessary until it can return without making the counter
   418      negative. If not given, value defaults to 1.
   419  
   420      """
   421      return _Semaphore(*args, **kwargs)
   422  
   423  class _Semaphore(_Verbose):
   424      """Semaphores manage a counter representing the number of release() calls
   425         minus the number of acquire() calls, plus an initial value. The acquire()
   426         method blocks if necessary until it can return without making the counter
   427         negative. If not given, value defaults to 1.
   428  
   429      """
   430  
   431      # After Tim Peters' semaphore class, but not quite the same (no maximum)
   432  
   433      def __init__(self, value=1, verbose=None):
   434          if value < 0:
   435              raise ValueError("semaphore initial value must be >= 0")
   436          _Verbose.__init__(self, verbose)
   437          self.__cond = Condition(Lock())
   438          self.__value = value
   439  
   440      def acquire(self, blocking=1):
   441          """Acquire a semaphore, decrementing the internal counter by one.
   442  
   443          When invoked without arguments: if the internal counter is larger than
   444          zero on entry, decrement it by one and return immediately. If it is zero
   445          on entry, block, waiting until some other thread has called release() to
   446          make it larger than zero. This is done with proper interlocking so that
   447          if multiple acquire() calls are blocked, release() will wake exactly one
   448          of them up. The implementation may pick one at random, so the order in
   449          which blocked threads are awakened should not be relied on. There is no
   450          return value in this case.
   451  
   452          When invoked with blocking set to true, do the same thing as when called
   453          without arguments, and return true.
   454  
   455          When invoked with blocking set to false, do not block. If a call without
   456          an argument would block, return false immediately; otherwise, do the
   457          same thing as when called without arguments, and return true.
   458  
   459          """
   460          rc = False
   461          with self.__cond:
   462              while self.__value == 0:
   463                  if not blocking:
   464                      break
   465                  if __debug__:
   466                      self._note("%s.acquire(%s): blocked waiting, value=%s",
   467                              self, blocking, self.__value)
   468                  self.__cond.wait()
   469              else:
   470                  self.__value = self.__value - 1
   471                  if __debug__:
   472                      self._note("%s.acquire: success, value=%s",
   473                              self, self.__value)
   474                  rc = True
   475          return rc
   476  
   477      __enter__ = acquire
   478  
   479      def release(self):
   480          """Release a semaphore, incrementing the internal counter by one.
   481  
   482          When the counter is zero on entry and another thread is waiting for it
   483          to become larger than zero again, wake up that thread.
   484  
   485          """
   486          with self.__cond:
   487              self.__value = self.__value + 1
   488              if __debug__:
   489                  self._note("%s.release: success, value=%s",
   490                          self, self.__value)
   491              self.__cond.notify()
   492  
   493      def __exit__(self, t, v, tb):
   494          self.release()
   495  
   496  
   497  def BoundedSemaphore(*args, **kwargs):
   498      """A factory function that returns a new bounded semaphore.
   499  
   500      A bounded semaphore checks to make sure its current value doesn't exceed its
   501      initial value. If it does, ValueError is raised. In most situations
   502      semaphores are used to guard resources with limited capacity.
   503  
   504      If the semaphore is released too many times it's a sign of a bug. If not
   505      given, value defaults to 1.
   506  
   507      Like regular semaphores, bounded semaphores manage a counter representing
   508      the number of release() calls minus the number of acquire() calls, plus an
   509      initial value. The acquire() method blocks if necessary until it can return
   510      without making the counter negative. If not given, value defaults to 1.
   511  
   512      """
   513      return _BoundedSemaphore(*args, **kwargs)
   514  
   515  class _BoundedSemaphore(_Semaphore):
   516      """A bounded semaphore checks to make sure its current value doesn't exceed
   517         its initial value. If it does, ValueError is raised. In most situations
   518         semaphores are used to guard resources with limited capacity.
   519      """
   520  
   521      def __init__(self, value=1, verbose=None):
   522          _Semaphore.__init__(self, value, verbose)
   523          self._initial_value = value
   524  
   525      def release(self):
   526          """Release a semaphore, incrementing the internal counter by one.
   527  
   528          When the counter is zero on entry and another thread is waiting for it
   529          to become larger than zero again, wake up that thread.
   530  
   531          If the number of releases exceeds the number of acquires,
   532          raise a ValueError.
   533  
   534          """
   535          with self.__cond:
   536              if self.__value >= self._initial_value:
   537                  raise ValueError("Semaphore released too many times")
   538              self.__value += 1
   539              self.__cond.notify()
   540  
   541  
   542  def Event(*args, **kwargs):
   543      """A factory function that returns a new event.
   544  
   545      Events manage a flag that can be set to true with the set() method and reset
   546      to false with the clear() method. The wait() method blocks until the flag is
   547      true.
   548  
   549      """
   550      return _Event(*args, **kwargs)
   551  
   552  class _Event(_Verbose):
   553      """A factory function that returns a new event object. An event manages a
   554         flag that can be set to true with the set() method and reset to false
   555         with the clear() method. The wait() method blocks until the flag is true.
   556  
   557      """
   558  
   559      # After Tim Peters' event class (without is_posted())
   560  
   561      def __init__(self, verbose=None):
   562          _Verbose.__init__(self, verbose)
   563          self.__cond = Condition(Lock())
   564          self.__flag = False
   565  
   566      def _reset_internal_locks(self):
   567          # private!  called by Thread._reset_internal_locks by _after_fork()
   568          self.__cond.__init__(Lock())
   569  
   570      def isSet(self):
   571          'Return true if and only if the internal flag is true.'
   572          return self.__flag
   573  
   574      is_set = isSet
   575  
   576      def set(self):
   577          """Set the internal flag to true.
   578  
   579          All threads waiting for the flag to become true are awakened. Threads
   580          that call wait() once the flag is true will not block at all.
   581  
   582          """
   583          with self.__cond:
   584              self.__flag = True
   585              self.__cond.notify_all()
   586  
   587      def clear(self):
   588          """Reset the internal flag to false.
   589  
   590          Subsequently, threads calling wait() will block until set() is called to
   591          set the internal flag to true again.
   592  
   593          """
   594          with self.__cond:
   595              self.__flag = False
   596  
   597      def wait(self, timeout=None):
   598          """Block until the internal flag is true.
   599  
   600          If the internal flag is true on entry, return immediately. Otherwise,
   601          block until another thread calls set() to set the flag to true, or until
   602          the optional timeout occurs.
   603  
   604          When the timeout argument is present and not None, it should be a
   605          floating point number specifying a timeout for the operation in seconds
   606          (or fractions thereof).
   607  
   608          This method returns the internal flag on exit, so it will always return
   609          True except if a timeout is given and the operation times out.
   610  
   611          """
   612          with self.__cond:
   613              if not self.__flag:
   614                  self.__cond.wait(timeout)
   615              return self.__flag
   616  
   617  # Helper to generate new thread names
   618  _counter = _count().next
   619  _counter() # Consume 0 so first non-main thread has id 1.
   620  def _newname(template="Thread-%d"):
   621      return template % _counter()
   622  
   623  # Active thread administration
   624  _active_limbo_lock = _allocate_lock()
   625  _active = {}    # maps thread id to Thread object
   626  _limbo = {}
   627  
   628  
   629  # Main class for threads
   630  
   631  class Thread(_Verbose):
   632      """A class that represents a thread of control.
   633  
   634      This class can be safely subclassed in a limited fashion.
   635  
   636      """
   637      __initialized = False
   638  
   639      def __init__(self, group=None, target=None, name=None,
   640                   args=(), kwargs=None, verbose=None):
   641          """This constructor should always be called with keyword arguments. Arguments are:
   642  
   643          *group* should be None; reserved for future extension when a ThreadGroup
   644          class is implemented.
   645  
   646          *target* is the callable object to be invoked by the run()
   647          method. Defaults to None, meaning nothing is called.
   648  
   649          *name* is the thread name. By default, a unique name is constructed of
   650          the form "Thread-N" where N is a small decimal number.
   651  
   652          *args* is the argument tuple for the target invocation. Defaults to ().
   653  
   654          *kwargs* is a dictionary of keyword arguments for the target
   655          invocation. Defaults to {}.
   656  
   657          If a subclass overrides the constructor, it must make sure to invoke
   658          the base class constructor (Thread.__init__()) before doing anything
   659          else to the thread.
   660  
   661  """
   662          assert group is None, "group argument must be None for now"
   663          _Verbose.__init__(self, verbose)
   664          if kwargs is None:
   665              kwargs = {}
   666          self.__target = target
   667          self.__name = str(name or _newname())
   668          self.__args = args
   669          self.__kwargs = kwargs
   670          self.__daemonic = self._set_daemon()
   671          self.__ident = None
   672          self.__started = Event()
   673          self.__stopped = False
   674          self.__block = Condition(Lock())
   675          self.__initialized = True
   676          # sys.stderr is not stored in the class like
   677          # sys.exc_info since it can be changed between instances
   678          self.__stderr = _sys.stderr
   679  
   680      def _reset_internal_locks(self):
   681          # private!  Called by _after_fork() to reset our internal locks as
   682          # they may be in an invalid state leading to a deadlock or crash.
   683          if hasattr(self, '__block'):  # DummyThread deletes self.__block
   684              self.__block.__init__()
   685          self.__started._reset_internal_locks()
   686  
   687      @property
   688      def _block(self):
   689          # used by a unittest
   690          return self.__block
   691  
   692      def _set_daemon(self):
   693          # Overridden in _MainThread and _DummyThread
   694          return current_thread().daemon
   695  
   696      def __repr__(self):
   697          assert self.__initialized, "Thread.__init__() was not called"
   698          status = "initial"
   699          if self.__started.is_set():
   700              status = "started"
   701          if self.__stopped:
   702              status = "stopped"
   703          if self.__daemonic:
   704              status += " daemon"
   705          if self.__ident is not None:
   706              status += " %s" % self.__ident
   707          return "<%s(%s, %s)>" % (self.__class__.__name__, self.__name, status)
   708  
   709      def start(self):
   710          """Start the thread's activity.
   711  
   712          It must be called at most once per thread object. It arranges for the
   713          object's run() method to be invoked in a separate thread of control.
   714  
   715          This method will raise a RuntimeError if called more than once on the
   716          same thread object.
   717  
   718          """
   719          if not self.__initialized:
   720              raise RuntimeError("thread.__init__() not called")
   721          if self.__started.is_set():
   722              raise RuntimeError("threads can only be started once")
   723          if __debug__:
   724              self._note("%s.start(): starting thread", self)
   725          with _active_limbo_lock:
   726              _limbo[self] = self
   727          try:
   728              _start_new_thread(self.__bootstrap, ())
   729          except Exception:
   730              with _active_limbo_lock:
   731                  del _limbo[self]
   732              raise
   733          self.__started.wait()
   734  
   735      def run(self):
   736          """Method representing the thread's activity.
   737  
   738          You may override this method in a subclass. The standard run() method
   739          invokes the callable object passed to the object's constructor as the
   740          target argument, if any, with sequential and keyword arguments taken
   741          from the args and kwargs arguments, respectively.
   742  
   743          """
   744          try:
   745              if self.__target:
   746                  self.__target(*self.__args, **self.__kwargs)
   747          finally:
   748              # Avoid a refcycle if the thread is running a function with
   749              # an argument that has a member that points to the thread.
   750              del self.__target, self.__args, self.__kwargs
   751  
   752      def __bootstrap(self):
   753          # Wrapper around the real bootstrap code that ignores
   754          # exceptions during interpreter cleanup.  Those typically
   755          # happen when a daemon thread wakes up at an unfortunate
   756          # moment, finds the world around it destroyed, and raises some
   757          # random exception *** while trying to report the exception in
   758          # __bootstrap_inner() below ***.  Those random exceptions
   759          # don't help anybody, and they confuse users, so we suppress
   760          # them.  We suppress them only when it appears that the world
   761          # indeed has already been destroyed, so that exceptions in
   762          # __bootstrap_inner() during normal business hours are properly
   763          # reported.  Also, we only suppress them for daemonic threads;
   764          # if a non-daemonic encounters this, something else is wrong.
   765          try:
   766              self.__bootstrap_inner()
   767          except:
   768              if self.__daemonic and _sys is None:
   769                  return
   770              raise
   771  
   772      def _set_ident(self):
   773          self.__ident = _get_ident()
   774  
   775      def __bootstrap_inner(self):
   776          try:
   777              self._set_ident()
   778              self.__started.set()
   779              with _active_limbo_lock:
   780                  _active[self.__ident] = self
   781                  del _limbo[self]
   782              if __debug__:
   783                  self._note("%s.__bootstrap(): thread started", self)
   784  
   785              if _trace_hook:
   786                  self._note("%s.__bootstrap(): registering trace hook", self)
   787                  _sys.settrace(_trace_hook)
   788              if _profile_hook:
   789                  self._note("%s.__bootstrap(): registering profile hook", self)
   790                  _sys.setprofile(_profile_hook)
   791  
   792              try:
   793                  self.run()
   794              except SystemExit:
   795                  if __debug__:
   796                      self._note("%s.__bootstrap(): raised SystemExit", self)
   797              except:
   798                  if __debug__:
   799                      self._note("%s.__bootstrap(): unhandled exception", self)
   800                  # If sys.stderr is no more (most likely from interpreter
   801                  # shutdown) use self.__stderr.  Otherwise still use sys (as in
   802                  # _sys) in case sys.stderr was redefined since the creation of
   803                  # self.
   804                  if _sys and _sys.stderr is not None:
   805                      print>>_sys.stderr, ("Exception in thread %s:\n%s" %
   806                                           (self.name, _format_exc()))
   807                  elif self.__stderr is not None:
   808                      # Do the best job possible w/o a huge amt. of code to
   809                      # approximate a traceback (code ideas from
   810                      # Lib/traceback.py)
   811                      exc_type, exc_value, exc_tb = _sys.exc_info()
   812                      try:
   813                          print>>self.__stderr, (
   814                              "Exception in thread " + self.name +
   815                              " (most likely raised during interpreter shutdown):")
   816                          print>>self.__stderr, (
   817                              "Traceback (most recent call last):")
   818                          while exc_tb:
   819                              print>>self.__stderr, (
   820                                  '  File "%s", line %s, in %s' %
   821                                  (exc_tb.tb_frame.f_code.co_filename,
   822                                      exc_tb.tb_lineno,
   823                                      exc_tb.tb_frame.f_code.co_name))
   824                              exc_tb = exc_tb.tb_next
   825                          print>>self.__stderr, ("%s: %s" % (exc_type, exc_value))
   826                      # Make sure that exc_tb gets deleted since it is a memory
   827                      # hog; deleting everything else is just for thoroughness
   828                      finally:
   829                          del exc_type, exc_value, exc_tb
   830              else:
   831                  if __debug__:
   832                      self._note("%s.__bootstrap(): normal return", self)
   833              finally:
   834                  # Prevent a race in
   835                  # test_threading.test_no_refcycle_through_target when
   836                  # the exception keeps the target alive past when we
   837                  # assert that it's dead.
   838                  _sys.exc_clear()
   839          finally:
   840              with _active_limbo_lock:
   841                  self.__stop()
   842                  try:
   843                      # We don't call self.__delete() because it also
   844                      # grabs _active_limbo_lock.
   845                      del _active[_get_ident()]
   846                  except:
   847                      pass
   848  
   849      def __stop(self):
   850          # DummyThreads delete self.__block, but they have no waiters to
   851          # notify anyway (join() is forbidden on them).
   852          if not hasattr(self, '__block'):
   853              return
   854          self.__block.acquire()
   855          self.__stopped = True
   856          self.__block.notify_all()
   857          self.__block.release()
   858  
   859      def __delete(self):
   860          "Remove current thread from the dict of currently running threads."
   861  
   862          # Notes about running with dummy_thread:
   863          #
   864          # Must take care to not raise an exception if dummy_thread is being
   865          # used (and thus this module is being used as an instance of
   866          # dummy_threading).  dummy_thread.get_ident() always returns -1 since
   867          # there is only one thread if dummy_thread is being used.  Thus
   868          # len(_active) is always <= 1 here, and any Thread instance created
   869          # overwrites the (if any) thread currently registered in _active.
   870          #
   871          # An instance of _MainThread is always created by 'threading'.  This
   872          # gets overwritten the instant an instance of Thread is created; both
   873          # threads return -1 from dummy_thread.get_ident() and thus have the
   874          # same key in the dict.  So when the _MainThread instance created by
   875          # 'threading' tries to clean itself up when atexit calls this method
   876          # it gets a KeyError if another Thread instance was created.
   877          #
   878          # This all means that KeyError from trying to delete something from
   879          # _active if dummy_threading is being used is a red herring.  But
   880          # since it isn't if dummy_threading is *not* being used then don't
   881          # hide the exception.
   882  
   883          try:
   884              with _active_limbo_lock:
   885                  del _active[_get_ident()]
   886                  # There must not be any python code between the previous line
   887                  # and after the lock is released.  Otherwise a tracing function
   888                  # could try to acquire the lock again in the same thread, (in
   889                  # current_thread()), and would block.
   890          except KeyError:
   891              if 'dummy_threading' not in _sys.modules:
   892                  raise
   893  
   894      def join(self, timeout=None):
   895          """Wait until the thread terminates.
   896  
   897          This blocks the calling thread until the thread whose join() method is
   898          called terminates -- either normally or through an unhandled exception
   899          or until the optional timeout occurs.
   900  
   901          When the timeout argument is present and not None, it should be a
   902          floating point number specifying a timeout for the operation in seconds
   903          (or fractions thereof). As join() always returns None, you must call
   904          isAlive() after join() to decide whether a timeout happened -- if the
   905          thread is still alive, the join() call timed out.
   906  
   907          When the timeout argument is not present or None, the operation will
   908          block until the thread terminates.
   909  
   910          A thread can be join()ed many times.
   911  
   912          join() raises a RuntimeError if an attempt is made to join the current
   913          thread as that would cause a deadlock. It is also an error to join() a
   914          thread before it has been started and attempts to do so raises the same
   915          exception.
   916  
   917          """
   918          if not self.__initialized:
   919              raise RuntimeError("Thread.__init__() not called")
   920          if not self.__started.is_set():
   921              raise RuntimeError("cannot join thread before it is started")
   922          if self is current_thread():
   923              raise RuntimeError("cannot join current thread")
   924  
   925          if __debug__:
   926              if not self.__stopped:
   927                  self._note("%s.join(): waiting until thread stops", self)
   928          self.__block.acquire()
   929          try:
   930              if timeout is None:
   931                  while not self.__stopped:
   932                      self.__block.wait()
   933                  if __debug__:
   934                      self._note("%s.join(): thread stopped", self)
   935              else:
   936                  deadline = _time() + timeout
   937                  while not self.__stopped:
   938                      delay = deadline - _time()
   939                      if delay <= 0:
   940                          if __debug__:
   941                              self._note("%s.join(): timed out", self)
   942                          break
   943                      self.__block.wait(delay)
   944                  else:
   945                      if __debug__:
   946                          self._note("%s.join(): thread stopped", self)
   947          finally:
   948              self.__block.release()
   949  
   950      def _name_getter(self):
   951          """A string used for identification purposes only.
   952  
   953          It has no semantics. Multiple threads may be given the same name. The
   954          initial name is set by the constructor.
   955  
   956          """
   957          assert self.__initialized, "Thread.__init__() not called"
   958          return self.__name
   959  
   960      def _name_setter(self, name):
   961          assert self.__initialized, "Thread.__init__() not called"
   962          self.__name = str(name)
   963  
   964      name = property(_name_getter, _name_setter)
   965  
   966      @property
   967      def ident(self):
   968          """Thread identifier of this thread or None if it has not been started.
   969  
   970          This is a nonzero integer. See the thread.get_ident() function. Thread
   971          identifiers may be recycled when a thread exits and another thread is
   972          created. The identifier is available even after the thread has exited.
   973  
   974          """
   975          assert self.__initialized, "Thread.__init__() not called"
   976          return self.__ident
   977  
   978      def isAlive(self):
   979          """Return whether the thread is alive.
   980  
   981          This method returns True just before the run() method starts until just
   982          after the run() method terminates. The module function enumerate()
   983          returns a list of all alive threads.
   984  
   985          """
   986          assert self.__initialized, "Thread.__init__() not called"
   987          return self.__started.is_set() and not self.__stopped
   988  
   989      is_alive = isAlive
   990  
   991      def _daemon_getter(self):
   992          """A boolean value indicating whether this thread is a daemon thread (True) or not (False).
   993  
   994          This must be set before start() is called, otherwise RuntimeError is
   995          raised. Its initial value is inherited from the creating thread; the
   996          main thread is not a daemon thread and therefore all threads created in
   997          the main thread default to daemon = False.
   998  
   999          The entire Python program exits when no alive non-daemon threads are
  1000          left.
  1001  
  1002          """
  1003          assert self.__initialized, "Thread.__init__() not called"
  1004          return self.__daemonic
  1005  
  1006      def _daemon_setter(self, daemonic):
  1007          if not self.__initialized:
  1008              raise RuntimeError("Thread.__init__() not called")
  1009          if self.__started.is_set():
  1010              raise RuntimeError("cannot set daemon status of active thread");
  1011          self.__daemonic = daemonic
  1012  
  1013      daemon = property(_daemon_getter, _daemon_setter)
  1014  
  1015      def isDaemon(self):
  1016          return self.daemon
  1017  
  1018      def setDaemon(self, daemonic):
  1019          self.daemon = daemonic
  1020  
  1021      def getName(self):
  1022          return self.name
  1023  
  1024      def setName(self, name):
  1025          self.name = name
  1026  
  1027  # The timer class was contributed by Itamar Shtull-Trauring
  1028  
  1029  def Timer(*args, **kwargs):
  1030      """Factory function to create a Timer object.
  1031  
  1032      Timers call a function after a specified number of seconds:
  1033  
  1034          t = Timer(30.0, f, args=[], kwargs={})
  1035          t.start()
  1036          t.cancel()     # stop the timer's action if it's still waiting
  1037  
  1038      """
  1039      return _Timer(*args, **kwargs)
  1040  
  1041  class _Timer(Thread):
  1042      """Call a function after a specified number of seconds:
  1043  
  1044              t = Timer(30.0, f, args=[], kwargs={})
  1045              t.start()
  1046              t.cancel()     # stop the timer's action if it's still waiting
  1047  
  1048      """
  1049  
  1050      def __init__(self, interval, function, args=[], kwargs={}):
  1051          Thread.__init__(self)
  1052          self.interval = interval
  1053          self.function = function
  1054          self.args = args
  1055          self.kwargs = kwargs
  1056          self.finished = Event()
  1057  
  1058      def cancel(self):
  1059          """Stop the timer if it hasn't finished yet"""
  1060          self.finished.set()
  1061  
  1062      def run(self):
  1063          self.finished.wait(self.interval)
  1064          if not self.finished.is_set():
  1065              self.function(*self.args, **self.kwargs)
  1066          self.finished.set()
  1067  
  1068  # Special thread class to represent the main thread
  1069  # This is garbage collected through an exit handler
  1070  
  1071  class _MainThread(Thread):
  1072  
  1073      def __init__(self):
  1074          Thread.__init__(self, name="MainThread")
  1075          self.__started.set()
  1076          self._set_ident()
  1077          with _active_limbo_lock:
  1078              _active[_get_ident()] = self
  1079  
  1080      def _set_daemon(self):
  1081          return False
  1082  
  1083      def _exitfunc(self):
  1084          self.__stop()
  1085          t = _pickSomeNonDaemonThread()
  1086          if t:
  1087              if __debug__:
  1088                  self._note("%s: waiting for other threads", self)
  1089          while t:
  1090              t.join()
  1091              t = _pickSomeNonDaemonThread()
  1092          if __debug__:
  1093              self._note("%s: exiting", self)
  1094          self.__delete()
  1095  
  1096  def _pickSomeNonDaemonThread():
  1097      for t in enumerate():
  1098          if not t.daemon and t.is_alive():
  1099              return t
  1100      return None
  1101  
  1102  
  1103  # Dummy thread class to represent threads not started here.
  1104  # These aren't garbage collected when they die, nor can they be waited for.
  1105  # If they invoke anything in threading.py that calls current_thread(), they
  1106  # leave an entry in the _active dict forever after.
  1107  # Their purpose is to return *something* from current_thread().
  1108  # They are marked as daemon threads so we won't wait for them
  1109  # when we exit (conform previous semantics).
  1110  
  1111  class _DummyThread(Thread):
  1112  
  1113      def __init__(self):
  1114          Thread.__init__(self, name=_newname("Dummy-%d"))
  1115  
  1116          # Thread.__block consumes an OS-level locking primitive, which
  1117          # can never be used by a _DummyThread.  Since a _DummyThread
  1118          # instance is immortal, that's bad, so release this resource.
  1119          del self.__block
  1120  
  1121          self.__started.set()
  1122          self._set_ident()
  1123          with _active_limbo_lock:
  1124              _active[_get_ident()] = self
  1125  
  1126      def _set_daemon(self):
  1127          return True
  1128  
  1129      def join(self, timeout=None):
  1130          assert False, "cannot join a dummy thread"
  1131  
  1132  
  1133  # Global API functions
  1134  
  1135  def currentThread():
  1136      """Return the current Thread object, corresponding to the caller's thread of control.
  1137  
  1138      If the caller's thread of control was not created through the threading
  1139      module, a dummy thread object with limited functionality is returned.
  1140  
  1141      """
  1142      try:
  1143          return _active[_get_ident()]
  1144      except KeyError:
  1145          ##print "current_thread(): no current thread for", _get_ident()
  1146          return _DummyThread()
  1147  
  1148  current_thread = currentThread
  1149  
  1150  def activeCount():
  1151      """Return the number of Thread objects currently alive.
  1152  
  1153      The returned count is equal to the length of the list returned by
  1154      enumerate().
  1155  
  1156      """
  1157      with _active_limbo_lock:
  1158          return len(_active) + len(_limbo)
  1159  
  1160  active_count = activeCount
  1161  
  1162  def _enumerate():
  1163      # Same as enumerate(), but without the lock. Internal use only.
  1164      return _active.values() + _limbo.values()
  1165  
  1166  def enumerate():
  1167      """Return a list of all Thread objects currently alive.
  1168  
  1169      The list includes daemonic threads, dummy thread objects created by
  1170      current_thread(), and the main thread. It excludes terminated threads and
  1171      threads that have not yet been started.
  1172  
  1173      """
  1174      with _active_limbo_lock:
  1175          return _active.values() + _limbo.values()
  1176  
  1177  from thread import stack_size
  1178  
  1179  # Create the main thread object,
  1180  # and make it available for the interpreter
  1181  # (Py_Main) as threading._shutdown.
  1182  
  1183  _shutdown = _MainThread()._exitfunc
  1184  
  1185  # get thread-local implementation, either from the thread
  1186  # module, or from the python fallback
  1187  
  1188  # NOTE: Thread local classes follow: the Grumpy version of this file copies
  1189  # these from _threading_local.py to avoid circular dependency issues.
  1190  
  1191  class _localbase(object):
  1192      __slots__ = '_local__key', '_local__args', '_local__lock'
  1193  
  1194      def __new__(cls, *args, **kw):
  1195          self = object.__new__(cls)
  1196          key = '_local__key', 'thread.local.' + str(id(self))
  1197          object.__setattr__(self, '_local__key', key)
  1198          object.__setattr__(self, '_local__args', (args, kw))
  1199          object.__setattr__(self, '_local__lock', RLock())
  1200  
  1201          if (args or kw) and (cls.__init__ is object.__init__):
  1202              raise TypeError("Initialization arguments are not supported")
  1203  
  1204          # We need to create the thread dict in anticipation of
  1205          # __init__ being called, to make sure we don't call it
  1206          # again ourselves.
  1207          dict = object.__getattribute__(self, '__dict__')
  1208          current_thread().__dict__[key] = dict
  1209  
  1210          return self
  1211  
  1212  def _patch(self):
  1213      key = object.__getattribute__(self, '_local__key')
  1214      d = current_thread().__dict__.get(key)
  1215      if d is None:
  1216          d = {}
  1217          current_thread().__dict__[key] = d
  1218          object.__setattr__(self, '__dict__', d)
  1219  
  1220          # we have a new instance dict, so call out __init__ if we have
  1221          # one
  1222          cls = type(self)
  1223          if cls.__init__ is not object.__init__:
  1224              args, kw = object.__getattribute__(self, '_local__args')
  1225              cls.__init__(self, *args, **kw)
  1226      else:
  1227          object.__setattr__(self, '__dict__', d)
  1228  
  1229  class local(_localbase):
  1230  
  1231      def __getattribute__(self, name):
  1232          lock = object.__getattribute__(self, '_local__lock')
  1233          lock.acquire()
  1234          try:
  1235              _patch(self)
  1236              return object.__getattribute__(self, name)
  1237          finally:
  1238              lock.release()
  1239  
  1240      def __setattr__(self, name, value):
  1241          if name == '__dict__':
  1242              raise AttributeError(
  1243                  "%r object attribute '__dict__' is read-only"
  1244                  % self.__class__.__name__)
  1245          lock = object.__getattribute__(self, '_local__lock')
  1246          lock.acquire()
  1247          try:
  1248              _patch(self)
  1249              return object.__setattr__(self, name, value)
  1250          finally:
  1251              lock.release()
  1252  
  1253      def __delattr__(self, name):
  1254          if name == '__dict__':
  1255              raise AttributeError(
  1256                  "%r object attribute '__dict__' is read-only"
  1257                  % self.__class__.__name__)
  1258          lock = object.__getattribute__(self, '_local__lock')
  1259          lock.acquire()
  1260          try:
  1261              _patch(self)
  1262              return object.__delattr__(self, name)
  1263          finally:
  1264              lock.release()
  1265  
  1266      def __del__(self):
  1267          key = object.__getattribute__(self, '_local__key')
  1268  
  1269          try:
  1270              # We use the non-locking API since we might already hold the lock
  1271              # (__del__ can be called at any point by the cyclic GC).
  1272              threads = _enumerate()
  1273          except:
  1274              # If enumerating the current threads fails, as it seems to do
  1275              # during shutdown, we'll skip cleanup under the assumption
  1276              # that there is nothing to clean up.
  1277              return
  1278  
  1279          for thread in threads:
  1280              try:
  1281                  __dict__ = thread.__dict__
  1282              except AttributeError:
  1283                  # Thread is dying, rest in peace.
  1284                  continue
  1285  
  1286              if key in __dict__:
  1287                  try:
  1288                      del __dict__[key]
  1289                  except KeyError:
  1290                      pass # didn't have anything in this thread
  1291  
  1292  # END _threading_local.py copy
  1293  
  1294  def _after_fork():
  1295      # This function is called by Python/ceval.c:PyEval_ReInitThreads which
  1296      # is called from PyOS_AfterFork.  Here we cleanup threading module state
  1297      # that should not exist after a fork.
  1298  
  1299      # Reset _active_limbo_lock, in case we forked while the lock was held
  1300      # by another (non-forked) thread.  http://bugs.python.org/issue874900
  1301      global _active_limbo_lock
  1302      _active_limbo_lock = _allocate_lock()
  1303  
  1304      # fork() only copied the current thread; clear references to others.
  1305      new_active = {}
  1306      current = current_thread()
  1307      with _active_limbo_lock:
  1308          for thread in _enumerate():
  1309              # Any lock/condition variable may be currently locked or in an
  1310              # invalid state, so we reinitialize them.
  1311              if hasattr(thread, '_reset_internal_locks'):
  1312                  thread._reset_internal_locks()
  1313              if thread is current:
  1314                  # There is only one active thread. We reset the ident to
  1315                  # its new value since it can have changed.
  1316                  ident = _get_ident()
  1317                  thread.__ident = ident
  1318                  new_active[ident] = thread
  1319              else:
  1320                  # All the others are already stopped.
  1321                  thread.__stop()
  1322  
  1323          _limbo.clear()
  1324          _active.clear()
  1325          _active.update(new_active)
  1326          assert len(_active) == 1
  1327  
  1328  
  1329  # Self-test code
  1330  
  1331  def _test():
  1332  
  1333      class BoundedQueue(_Verbose):
  1334  
  1335          def __init__(self, limit):
  1336              _Verbose.__init__(self)
  1337              self.mon = RLock()
  1338              self.rc = Condition(self.mon)
  1339              self.wc = Condition(self.mon)
  1340              self.limit = limit
  1341              self.queue = _deque()
  1342  
  1343          def put(self, item):
  1344              self.mon.acquire()
  1345              while len(self.queue) >= self.limit:
  1346                  self._note("put(%s): queue full", item)
  1347                  self.wc.wait()
  1348              self.queue.append(item)
  1349              self._note("put(%s): appended, length now %d",
  1350                         item, len(self.queue))
  1351              self.rc.notify()
  1352              self.mon.release()
  1353  
  1354          def get(self):
  1355              self.mon.acquire()
  1356              while not self.queue:
  1357                  self._note("get(): queue empty")
  1358                  self.rc.wait()
  1359              item = self.queue.popleft()
  1360              self._note("get(): got %s, %d left", item, len(self.queue))
  1361              self.wc.notify()
  1362              self.mon.release()
  1363              return item
  1364  
  1365      class ProducerThread(Thread):
  1366  
  1367          def __init__(self, queue, quota):
  1368              Thread.__init__(self, name="Producer")
  1369              self.queue = queue
  1370              self.quota = quota
  1371  
  1372          def run(self):
  1373              from random import random
  1374              counter = 0
  1375              while counter < self.quota:
  1376                  counter = counter + 1
  1377                  self.queue.put("%s.%d" % (self.name, counter))
  1378                  _sleep(random() * 0.00001)
  1379  
  1380  
  1381      class ConsumerThread(Thread):
  1382  
  1383          def __init__(self, queue, count):
  1384              Thread.__init__(self, name="Consumer")
  1385              self.queue = queue
  1386              self.count = count
  1387  
  1388          def run(self):
  1389              while self.count > 0:
  1390                  item = self.queue.get()
  1391                  print item
  1392                  self.count = self.count - 1
  1393  
  1394      NP = 3
  1395      QL = 4
  1396      NI = 5
  1397  
  1398      Q = BoundedQueue(QL)
  1399      P = []
  1400      for i in range(NP):
  1401          t = ProducerThread(Q, NI)
  1402          t.name = ("Producer-%d" % (i+1))
  1403          P.append(t)
  1404      C = ConsumerThread(Q, NI*NP)
  1405      for t in P:
  1406          t.start()
  1407          _sleep(0.000001)
  1408      C.start()
  1409      for t in P:
  1410          t.join()
  1411      C.join()
  1412  
  1413  if __name__ == '__main__':
  1414      _test()