home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2012 January / maximum-cd-2012-01.iso / DiscContents / digsby_setup.exe / lib / util / threads / bgthread.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2011-10-05  |  5.2 KB  |  166 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. from __future__ import with_statement
  5. import util.primitives.funcs as funcs
  6. import threading
  7. from Queue import Queue
  8. from functools import wraps
  9. from traceback import print_exc
  10. __all__ = [
  11.     'BackgroundThread',
  12.     'add_before_cb',
  13.     'add_before_cb']
  14.  
  15. class DelegateThread(threading.Thread):
  16.     
  17.     def __init__(self, group = None, target = None, name = None, args = (), kwargs = None, verbose = None):
  18.         threading.Thread.__init__(self, group, target, name, args, kwargs, verbose)
  19.         self.BeforeRun = funcs.Delegate()
  20.         self.AfterRun = funcs.Delegate()
  21.  
  22.  
  23.  
  24. class BackgroundThread(DelegateThread):
  25.     
  26.     def __init__(self, group = None, target = None, name = None, args = (), kwargs = None, verbose = None):
  27.         DelegateThread.__init__(self, group, target, name, args, kwargs, verbose)
  28.         self.setDaemon(True)
  29.         self.BeforeRun[:] = _before_run
  30.         self.AfterRun[:] = _after_run
  31.  
  32.  
  33. _before_run = []
  34. _after_run = []
  35.  
  36. def add_before_cb(cb):
  37.     _before_run.append(cb)
  38.  
  39.  
  40. def add_after_cb(cb):
  41.     _after_run.append(cb)
  42.  
  43. from thread import get_ident
  44.  
  45. class on_thread(object):
  46.     threads = { }
  47.     lock = threading.RLock()
  48.     
  49.     def __init__(self, name, daemon = True):
  50.         self.name = name
  51.         self.daemon = daemon
  52.         
  53.         try:
  54.             self._id = self.threads[self.name].ident
  55.         except KeyError:
  56.             self._id = -1
  57.  
  58.  
  59.     
  60.     def thread(self):
  61.         
  62.         try:
  63.             return self._thread
  64.         except AttributeError:
  65.             self.lock.__enter__()
  66.             
  67.             try:
  68.                 self._thread = self.threads[self.name]
  69.             except KeyError:
  70.                 self.lock.__exit__
  71.                 self.lock.__exit__
  72.                 self.lock
  73.                 self._thread = self.threads[self.name] = on_thread_thread(self.name, daemon = self.daemon)
  74.                 self._thread.start()
  75.                 self._id = self._thread.ident
  76.             except:
  77.                 self.lock.__exit__
  78.             finally:
  79.                 pass
  80.  
  81.             return self._thread
  82.             self.lock.__exit__
  83.  
  84.  
  85.     thread = property(thread)
  86.     
  87.     def _done(self, thread_name):
  88.         self.lock.__enter__()
  89.         
  90.         try:
  91.             self.threads.pop(thread_name)
  92.         finally:
  93.             pass
  94.  
  95.  
  96.     
  97.     def now(self):
  98.         return self._id == get_ident()
  99.  
  100.     now = property(now)
  101.     
  102.     def call(self, func, *a, **k):
  103.         self.thread.queue(func, *a, **k)
  104.  
  105.     
  106.     def __call__(self, func):
  107.         
  108.         def wrapper(*a, **k):
  109.             self.call(func, *a, **k)
  110.  
  111.         wrapper = (None, wraps(func))(wrapper)
  112.         wrapper.on_thread = self
  113.         return wrapper
  114.  
  115.  
  116.  
  117. try:
  118.     from wx import SEHGuard
  119. except ImportError:
  120.     
  121.     SEHGuard = lambda c: c()
  122.  
  123.  
  124. class on_thread_thread(BackgroundThread):
  125.     
  126.     def __init__(self, name, daemon = True):
  127.         BackgroundThread.__init__(self, name = name)
  128.         self.setDaemon(daemon)
  129.         self.work = Queue()
  130.         self.done = False
  131.  
  132.     
  133.     def run(self):
  134.         self.BeforeRun()
  135.         
  136.         try:
  137.             SEHGuard(self._consumer_loop)
  138.         finally:
  139.             self.AfterRun()
  140.  
  141.  
  142.     
  143.     def _consumer_loop(self):
  144.         while not self.done:
  145.             setattr(self, 'loopcount', getattr(self, 'loopcount', 0) + 1)
  146.             (func, args, kwargs) = self.work.get()
  147.             
  148.             try:
  149.                 func(*args, **kwargs)
  150.             except Exception:
  151.                 print_exc()
  152.  
  153.             self.work.task_done()
  154.         on_thread(self.name)._done()
  155.  
  156.     
  157.     def queue(self, func, *a, **k):
  158.         self.work.put((func, a, k))
  159.  
  160.     
  161.     def join(self):
  162.         self.done = True
  163.         self.work.join()
  164.  
  165.  
  166.