home *** CD-ROM | disk | FTP | other *** search
/ PC World 2002 May / PCWorld_2002-05_cd.bin / Software / TemaCD / activepython / ActivePython-2.1.1.msi / Python21_Lib_threading.py < prev    next >
Encoding:
Python Source  |  2001-07-26  |  18.3 KB  |  634 lines

  1. """Proposed new threading module, emulating a subset of Java's threading model."""
  2.  
  3. import sys
  4. import time
  5. import thread
  6. import traceback
  7. import StringIO
  8.  
  9. # Rename some stuff so "from threading import *" is safe
  10.  
  11. _sys = sys
  12. del sys
  13.  
  14. _time = time.time
  15. _sleep = time.sleep
  16. del time
  17.  
  18. _start_new_thread = thread.start_new_thread
  19. _allocate_lock = thread.allocate_lock
  20. _get_ident = thread.get_ident
  21. ThreadError = thread.error
  22. del thread
  23.  
  24. _print_exc = traceback.print_exc
  25. del traceback
  26.  
  27. _StringIO = StringIO.StringIO
  28. del StringIO
  29.  
  30.  
  31. # Debug support (adapted from ihooks.py)
  32.  
  33. _VERBOSE = 0
  34.  
  35. if __debug__:
  36.  
  37.     class _Verbose:
  38.  
  39.         def __init__(self, verbose=None):
  40.             if verbose is None:
  41.                 verbose = _VERBOSE
  42.             self.__verbose = verbose
  43.  
  44.         def _note(self, format, *args):
  45.             if self.__verbose:
  46.                 format = format % args
  47.                 format = "%s: %s\n" % (
  48.                     currentThread().getName(), format)
  49.                 _sys.stderr.write(format)
  50.  
  51. else:
  52.     # Disable this when using "python -O"
  53.     class _Verbose:
  54.         def __init__(self, verbose=None):
  55.             pass
  56.         def _note(self, *args):
  57.             pass
  58.  
  59.  
  60. # Synchronization classes
  61.  
  62. Lock = _allocate_lock
  63.  
  64. def RLock(*args, **kwargs):
  65.     return apply(_RLock, args, kwargs)
  66.  
  67. class _RLock(_Verbose):
  68.  
  69.     def __init__(self, verbose=None):
  70.         _Verbose.__init__(self, verbose)
  71.         self.__block = _allocate_lock()
  72.         self.__owner = None
  73.         self.__count = 0
  74.  
  75.     def __repr__(self):
  76.         return "<%s(%s, %d)>" % (
  77.                 self.__class__.__name__,
  78.                 self.__owner and self.__owner.getName(),
  79.                 self.__count)
  80.  
  81.     def acquire(self, blocking=1):
  82.         me = currentThread()
  83.         if self.__owner is me:
  84.             self.__count = self.__count + 1
  85.             if __debug__:
  86.                 self._note("%s.acquire(%s): recursive success", self, blocking)
  87.             return 1
  88.         rc = self.__block.acquire(blocking)
  89.         if rc:
  90.             self.__owner = me
  91.             self.__count = 1
  92.             if __debug__:
  93.                 self._note("%s.acquire(%s): initial succes", self, blocking)
  94.         else:
  95.             if __debug__:
  96.                 self._note("%s.acquire(%s): failure", self, blocking)
  97.         return rc
  98.  
  99.     def release(self):
  100.         me = currentThread()
  101.         assert self.__owner is me, "release() of un-acquire()d lock"
  102.         self.__count = count = self.__count - 1
  103.         if not count:
  104.             self.__owner = None
  105.             self.__block.release()
  106.             if __debug__:
  107.                 self._note("%s.release(): final release", self)
  108.         else:
  109.             if __debug__:
  110.                 self._note("%s.release(): non-final release", self)
  111.  
  112.     # Internal methods used by condition variables
  113.  
  114.     def _acquire_restore(self, (count, owner)):
  115.         self.__block.acquire()
  116.         self.__count = count
  117.         self.__owner = owner
  118.         if __debug__:
  119.             self._note("%s._acquire_restore()", self)
  120.  
  121.     def _release_save(self):
  122.         if __debug__:
  123.             self._note("%s._release_save()", self)
  124.         count = self.__count
  125.         self.__count = 0
  126.         owner = self.__owner
  127.         self.__owner = None
  128.         self.__block.release()
  129.         return (count, owner)
  130.  
  131.     def _is_owned(self):
  132.         return self.__owner is currentThread()
  133.  
  134.  
  135. def Condition(*args, **kwargs):
  136.     return apply(_Condition, args, kwargs)
  137.  
  138. class _Condition(_Verbose):
  139.  
  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.  
  165.     def __repr__(self):
  166.         return "<Condition(%s, %d)>" % (self.__lock, len(self.__waiters))
  167.  
  168.     def _release_save(self):
  169.         self.__lock.release()           # No state to save
  170.  
  171.     def _acquire_restore(self, x):
  172.         self.__lock.acquire()           # Ignore saved state
  173.  
  174.     def _is_owned(self):
  175.         if self.__lock.acquire(0):
  176.             self.__lock.release()
  177.             return 0
  178.         else:
  179.             return 1
  180.  
  181.     def wait(self, timeout=None):
  182.         me = currentThread()
  183.         assert self._is_owned(), "wait() of un-acquire()d lock"
  184.         waiter = _allocate_lock()
  185.         waiter.acquire()
  186.         self.__waiters.append(waiter)
  187.         saved_state = self._release_save()
  188.         try:    # restore state no matter what (e.g., KeyboardInterrupt)
  189.             if timeout is None:
  190.                 waiter.acquire()
  191.                 if __debug__:
  192.                     self._note("%s.wait(): got it", self)
  193.             else:
  194.                 endtime = _time() + timeout
  195.                 delay = 0.000001 # 1 usec
  196.                 while 1:
  197.                     gotit = waiter.acquire(0)
  198.                     if gotit or _time() >= endtime:
  199.                         break
  200.                     _sleep(delay)
  201.                     if delay < 1.0:
  202.                         delay = delay * 2.0
  203.                 if not gotit:
  204.                     if __debug__:
  205.                         self._note("%s.wait(%s): timed out", self, timeout)
  206.                     try:
  207.                         self.__waiters.remove(waiter)
  208.                     except ValueError:
  209.                         pass
  210.                 else:
  211.                     if __debug__:
  212.                         self._note("%s.wait(%s): got it", self, timeout)
  213.         finally:
  214.             self._acquire_restore(saved_state)
  215.  
  216.     def notify(self, n=1):
  217.         me = currentThread()
  218.         assert self._is_owned(), "notify() of un-acquire()d lock"
  219.         __waiters = self.__waiters
  220.         waiters = __waiters[:n]
  221.         if not waiters:
  222.             if __debug__:
  223.                 self._note("%s.notify(): no waiters", self)
  224.             return
  225.         self._note("%s.notify(): notifying %d waiter%s", self, n,
  226.                    n!=1 and "s" or "")
  227.         for waiter in waiters:
  228.             waiter.release()
  229.             try:
  230.                 __waiters.remove(waiter)
  231.             except ValueError:
  232.                 pass
  233.  
  234.     def notifyAll(self):
  235.         self.notify(len(self.__waiters))
  236.  
  237.  
  238. def Semaphore(*args, **kwargs):
  239.     return apply(_Semaphore, args, kwargs)
  240.  
  241. class _Semaphore(_Verbose):
  242.  
  243.     # After Tim Peters' semaphore class, but not quite the same (no maximum)
  244.  
  245.     def __init__(self, value=1, verbose=None):
  246.         assert value >= 0, "Semaphore initial value must be >= 0"
  247.         _Verbose.__init__(self, verbose)
  248.         self.__cond = Condition(Lock())
  249.         self.__value = value
  250.  
  251.     def acquire(self, blocking=1):
  252.         rc = 0
  253.         self.__cond.acquire()
  254.         while self.__value == 0:
  255.             if not blocking:
  256.                 break
  257.             self.__cond.wait()
  258.         else:
  259.             self.__value = self.__value - 1
  260.             rc = 1
  261.         self.__cond.release()
  262.         return rc
  263.  
  264.     def release(self):
  265.         self.__cond.acquire()
  266.         self.__value = self.__value + 1
  267.         self.__cond.notify()
  268.         self.__cond.release()
  269.  
  270.  
  271. def Event(*args, **kwargs):
  272.     return apply(_Event, args, kwargs)
  273.  
  274. class _Event(_Verbose):
  275.  
  276.     # After Tim Peters' event class (without is_posted())
  277.  
  278.     def __init__(self, verbose=None):
  279.         _Verbose.__init__(self, verbose)
  280.         self.__cond = Condition(Lock())
  281.         self.__flag = 0
  282.  
  283.     def isSet(self):
  284.         return self.__flag
  285.  
  286.     def set(self):
  287.         self.__cond.acquire()
  288.         self.__flag = 1
  289.         self.__cond.notifyAll()
  290.         self.__cond.release()
  291.  
  292.     def clear(self):
  293.         self.__cond.acquire()
  294.         self.__flag = 0
  295.         self.__cond.release()
  296.  
  297.     def wait(self, timeout=None):
  298.         self.__cond.acquire()
  299.         if not self.__flag:
  300.             self.__cond.wait(timeout)
  301.         self.__cond.release()
  302.  
  303.  
  304. # Helper to generate new thread names
  305. _counter = 0
  306. def _newname(template="Thread-%d"):
  307.     global _counter
  308.     _counter = _counter + 1
  309.     return template % _counter
  310.  
  311. # Active thread administration
  312. _active_limbo_lock = _allocate_lock()
  313. _active = {}
  314. _limbo = {}
  315.  
  316.  
  317. # Main class for threads
  318.  
  319. class Thread(_Verbose):
  320.  
  321.     __initialized = 0
  322.  
  323.     def __init__(self, group=None, target=None, name=None,
  324.                  args=(), kwargs={}, verbose=None):
  325.         assert group is None, "group argument must be None for now"
  326.         _Verbose.__init__(self, verbose)
  327.         self.__target = target
  328.         self.__name = str(name or _newname())
  329.         self.__args = args
  330.         self.__kwargs = kwargs
  331.         self.__daemonic = self._set_daemon()
  332.         self.__started = 0
  333.         self.__stopped = 0
  334.         self.__block = Condition(Lock())
  335.         self.__initialized = 1
  336.  
  337.     def _set_daemon(self):
  338.         # Overridden in _MainThread and _DummyThread
  339.         return currentThread().isDaemon()
  340.  
  341.     def __repr__(self):
  342.         assert self.__initialized, "Thread.__init__() was not called"
  343.         status = "initial"
  344.         if self.__started:
  345.             status = "started"
  346.         if self.__stopped:
  347.             status = "stopped"
  348.         if self.__daemonic:
  349.             status = status + " daemon"
  350.         return "<%s(%s, %s)>" % (self.__class__.__name__, self.__name, status)
  351.  
  352.     def start(self):
  353.         assert self.__initialized, "Thread.__init__() not called"
  354.         assert not self.__started, "thread already started"
  355.         if __debug__:
  356.             self._note("%s.start(): starting thread", self)
  357.         _active_limbo_lock.acquire()
  358.         _limbo[self] = self
  359.         _active_limbo_lock.release()
  360.         _start_new_thread(self.__bootstrap, ())
  361.         self.__started = 1
  362.         _sleep(0.000001)    # 1 usec, to let the thread run (Solaris hack)
  363.  
  364.     def run(self):
  365.         if self.__target:
  366.             apply(self.__target, self.__args, self.__kwargs)
  367.  
  368.     def __bootstrap(self):
  369.         try:
  370.             self.__started = 1
  371.             _active_limbo_lock.acquire()
  372.             _active[_get_ident()] = self
  373.             del _limbo[self]
  374.             _active_limbo_lock.release()
  375.             if __debug__:
  376.                 self._note("%s.__bootstrap(): thread started", self)
  377.             try:
  378.                 self.run()
  379.             except SystemExit:
  380.                 if __debug__:
  381.                     self._note("%s.__bootstrap(): raised SystemExit", self)
  382.             except:
  383.                 if __debug__:
  384.                     self._note("%s.__bootstrap(): unhandled exception", self)
  385.                 s = _StringIO()
  386.                 _print_exc(file=s)
  387.                 _sys.stderr.write("Exception in thread %s:\n%s\n" %
  388.                                  (self.getName(), s.getvalue()))
  389.             else:
  390.                 if __debug__:
  391.                     self._note("%s.__bootstrap(): normal return", self)
  392.         finally:
  393.             self.__stop()
  394.             self.__delete()
  395.  
  396.     def __stop(self):
  397.         self.__block.acquire()
  398.         self.__stopped = 1
  399.         self.__block.notifyAll()
  400.         self.__block.release()
  401.  
  402.     def __delete(self):
  403.         _active_limbo_lock.acquire()
  404.         del _active[_get_ident()]
  405.         _active_limbo_lock.release()
  406.  
  407.     def join(self, timeout=None):
  408.         assert self.__initialized, "Thread.__init__() not called"
  409.         assert self.__started, "cannot join thread before it is started"
  410.         assert self is not currentThread(), "cannot join current thread"
  411.         if __debug__:
  412.             if not self.__stopped:
  413.                 self._note("%s.join(): waiting until thread stops", self)
  414.         self.__block.acquire()
  415.         if timeout is None:
  416.             while not self.__stopped:
  417.                 self.__block.wait()
  418.             if __debug__:
  419.                 self._note("%s.join(): thread stopped", self)
  420.         else:
  421.             deadline = _time() + timeout
  422.             while not self.__stopped:
  423.                 delay = deadline - _time()
  424.                 if delay <= 0:
  425.                     if __debug__:
  426.                         self._note("%s.join(): timed out", self)
  427.                     break
  428.                 self.__block.wait(delay)
  429.             else:
  430.                 if __debug__:
  431.                     self._note("%s.join(): thread stopped", self)
  432.         self.__block.release()
  433.  
  434.     def getName(self):
  435.         assert self.__initialized, "Thread.__init__() not called"
  436.         return self.__name
  437.  
  438.     def setName(self, name):
  439.         assert self.__initialized, "Thread.__init__() not called"
  440.         self.__name = str(name)
  441.  
  442.     def isAlive(self):
  443.         assert self.__initialized, "Thread.__init__() not called"
  444.         return self.__started and not self.__stopped
  445.  
  446.     def isDaemon(self):
  447.         assert self.__initialized, "Thread.__init__() not called"
  448.         return self.__daemonic
  449.  
  450.     def setDaemon(self, daemonic):
  451.         assert self.__initialized, "Thread.__init__() not called"
  452.         assert not self.__started, "cannot set daemon status of active thread"
  453.         self.__daemonic = daemonic
  454.  
  455.  
  456. # Special thread class to represent the main thread
  457. # This is garbage collected through an exit handler
  458.  
  459. class _MainThread(Thread):
  460.  
  461.     def __init__(self):
  462.         Thread.__init__(self, name="MainThread")
  463.         self._Thread__started = 1
  464.         _active_limbo_lock.acquire()
  465.         _active[_get_ident()] = self
  466.         _active_limbo_lock.release()
  467.         import atexit
  468.         atexit.register(self.__exitfunc)
  469.  
  470.     def _set_daemon(self):
  471.         return 0
  472.  
  473.     def __exitfunc(self):
  474.         self._Thread__stop()
  475.         t = _pickSomeNonDaemonThread()
  476.         if t:
  477.             if __debug__:
  478.                 self._note("%s: waiting for other threads", self)
  479.         while t:
  480.             t.join()
  481.             t = _pickSomeNonDaemonThread()
  482.         if __debug__:
  483.             self._note("%s: exiting", self)
  484.         self._Thread__delete()
  485.  
  486. def _pickSomeNonDaemonThread():
  487.     for t in enumerate():
  488.         if not t.isDaemon() and t.isAlive():
  489.             return t
  490.     return None
  491.  
  492.  
  493. # Dummy thread class to represent threads not started here.
  494. # These aren't garbage collected when they die,
  495. # nor can they be waited for.
  496. # Their purpose is to return *something* from currentThread().
  497. # They are marked as daemon threads so we won't wait for them
  498. # when we exit (conform previous semantics).
  499.  
  500. class _DummyThread(Thread):
  501.  
  502.     def __init__(self):
  503.         Thread.__init__(self, name=_newname("Dummy-%d"))
  504.         self._Thread__started = 1
  505.         _active_limbo_lock.acquire()
  506.         _active[_get_ident()] = self
  507.         _active_limbo_lock.release()
  508.  
  509.     def _set_daemon(self):
  510.         return 1
  511.  
  512.     def join(self):
  513.         assert 0, "cannot join a dummy thread"
  514.  
  515.  
  516. # Global API functions
  517.  
  518. def currentThread():
  519.     try:
  520.         return _active[_get_ident()]
  521.     except KeyError:
  522.         ##print "currentThread(): no current thread for", _get_ident()
  523.         return _DummyThread()
  524.  
  525. def activeCount():
  526.     _active_limbo_lock.acquire()
  527.     count = len(_active) + len(_limbo)
  528.     _active_limbo_lock.release()
  529.     return count
  530.  
  531. def enumerate():
  532.     _active_limbo_lock.acquire()
  533.     active = _active.values() + _limbo.values()
  534.     _active_limbo_lock.release()
  535.     return active
  536.  
  537.  
  538. # Create the main thread object
  539.  
  540. _MainThread()
  541.  
  542.  
  543. # Self-test code
  544.  
  545. def _test():
  546.  
  547.     import random
  548.  
  549.     class BoundedQueue(_Verbose):
  550.  
  551.         def __init__(self, limit):
  552.             _Verbose.__init__(self)
  553.             self.mon = RLock()
  554.             self.rc = Condition(self.mon)
  555.             self.wc = Condition(self.mon)
  556.             self.limit = limit
  557.             self.queue = []
  558.  
  559.         def put(self, item):
  560.             self.mon.acquire()
  561.             while len(self.queue) >= self.limit:
  562.                 self._note("put(%s): queue full", item)
  563.                 self.wc.wait()
  564.             self.queue.append(item)
  565.             self._note("put(%s): appended, length now %d",
  566.                        item, len(self.queue))
  567.             self.rc.notify()
  568.             self.mon.release()
  569.  
  570.         def get(self):
  571.             self.mon.acquire()
  572.             while not self.queue:
  573.                 self._note("get(): queue empty")
  574.                 self.rc.wait()
  575.             item = self.queue[0]
  576.             del self.queue[0]
  577.             self._note("get(): got %s, %d left", item, len(self.queue))
  578.             self.wc.notify()
  579.             self.mon.release()
  580.             return item
  581.  
  582.     class ProducerThread(Thread):
  583.  
  584.         def __init__(self, queue, quota):
  585.             Thread.__init__(self, name="Producer")
  586.             self.queue = queue
  587.             self.quota = quota
  588.  
  589.         def run(self):
  590.             from random import random
  591.             counter = 0
  592.             while counter < self.quota:
  593.                 counter = counter + 1
  594.                 self.queue.put("%s.%d" % (self.getName(), counter))
  595.                 _sleep(random() * 0.00001)
  596.  
  597.  
  598.     class ConsumerThread(Thread):
  599.  
  600.         def __init__(self, queue, count):
  601.             Thread.__init__(self, name="Consumer")
  602.             self.queue = queue
  603.             self.count = count
  604.  
  605.         def run(self):
  606.             while self.count > 0:
  607.                 item = self.queue.get()
  608.                 print item
  609.                 self.count = self.count - 1
  610.  
  611.     import time
  612.  
  613.     NP = 3
  614.     QL = 4
  615.     NI = 5
  616.  
  617.     Q = BoundedQueue(QL)
  618.     P = []
  619.     for i in range(NP):
  620.         t = ProducerThread(Q, NI)
  621.         t.setName("Producer-%d" % (i+1))
  622.         P.append(t)
  623.     C = ConsumerThread(Q, NI*NP)
  624.     for t in P:
  625.         t.start()
  626.         _sleep(0.000001)
  627.     C.start()
  628.     for t in P:
  629.         t.join()
  630.     C.join()
  631.  
  632. if __name__ == '__main__':
  633.     _test()
  634.