Python event signal scheduling

From , 4 Years ago, written in Python, viewed 227 times.
URL https://pastebin.vip/view/b7046757
  1. #idle_queue.py
  2.  
  3. import Queue
  4.  
  5. #Global queue, import it from anywhere, you get the same object instance.
  6. idle_loop = Queue.Queue()
  7.  
  8. def idle_add(func, *args, **kwargs):
  9.     def idle():
  10.         func(*args, **kwargs)
  11.         return False
  12.     idle_loop.put(idle)
  13.  
  14.  
  15. #weak_ref.py
  16.  
  17. import weakref
  18.  
  19.  
  20. class _BoundMethodWeakref:
  21.     def __init__(self, func):
  22.         self.func_name = func.__name__
  23.         self.wref = weakref.ref(func.__self__) #__self__ returns the class
  24.  
  25.     def __call__(self):
  26.         func_cls = self.wref()
  27.         if func_cls is None: #lost reference
  28.             return None
  29.         else:
  30.             func = getattr(func_cls, self.func_name)
  31.             return func
  32.  
  33. def weak_ref(callback):
  34.     if hasattr(callback, '__self__') and callback.__self__ is not None: #is a bound method?
  35.         return _BoundMethodWeakref(callback)
  36.     else:
  37.         return weakref.ref(callback)
  38.  
  39.  
  40. #event.py
  41.  
  42. import threading
  43. #import logging
  44. #logger = logging.getLogger(__name__)
  45.  
  46. import idle_queue
  47. from weak_ref import weak_ref
  48.  
  49.  
  50. class Event:
  51.     def __init__(self, name):
  52.         self.name = name
  53.         self.callbacks = []
  54.         self.lock = threading.Lock()
  55.  
  56.     def connect(self, callback):
  57.         with self.lock:
  58.             callback = weak_ref(callback)
  59.             self.callbacks.append(callback)
  60.  
  61.     def disconnect(self, callback):
  62.         with self.lock:
  63.             for index, weakref_callback in enumerate(self.callbacks):
  64.                 if callback == weakref_callback():
  65.                     del self.callbacks[index]
  66.                     break
  67.  
  68.     def emit(self, *args, **kwargs):
  69.         with self.lock:
  70.             #logger.debug("Event emitted: {}".format(self.name))
  71.             for weakref_callback in self.callbacks[:]:
  72.                 callback = weakref_callback()
  73.                 if callback is not None:
  74.                     idle_queue.idle_add(callback, *args, **kwargs)
  75.                 else: #lost reference
  76.                     self.callbacks.remove(weakref_callback)
  77.             #if not self.callbacks:
  78.                 #logger.debug("No signals assosiated to: {}".format(self.name))
  79.  
  80.  
  81. #events.py
  82.  
  83. import Event
  84.  
  85.  
  86. class _Events:
  87.     #add some signals, example:
  88.     #do_something = Event('do something') #args: my_arg1, my_arg_list2, my_arg_str3
  89.     quit_app = Event('quit app') #args: some_arg
  90.  
  91. events = _Events()
  92.  
  93. if __name__ == "__main__":
  94.     def quit(some_arg):
  95.         print some_arg
  96.         sys.exit(0)
  97.  
  98.     events.quit_app.connect(quit) #connect the callback/slot
  99.     #events.quit_app.disconnect(quit) #disconnect
  100.     something = "goodbye"
  101.     events.quit_app.emit(something) #emit the signal
  102.    
  103.     #this should go in your main thread loop if your are using a gui.
  104.     #example: http://code.activestate.com/recipes/578299-pyqt-pyside-thread-safe-global-queue-main-loop-int/
  105.     callback = idle_loop.get()
  106.     callback() #dispatch the event
  107. #//python/5594

Reply to "Python event signal scheduling"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website