Exemplo n.º 1
0
 def __init__(self):
     """
     Creates new EventReactor with standard SynchronizedQueue and __QueueOperator
     """
     self.__event_queue = SynchronizedQueue()
     self.__enqueuer = __QueueOperator(self.__event_queue)
     
     self.__rshift__ = self.__enqueuer.__rshift__
     self.__lshift__ = self.__enqueuer.__lshift__
     
     self.__last_access_time = sys.current_time()
Exemplo n.º 2
0
class EventReactor(object):
    """
    EventReactor is base class for all EventReactors.
    Provides a way to queue new events and then EventReactor deals with
    the events
    """
    
    def __init__(self):
        """
        Creates new EventReactor with standard SynchronizedQueue and __QueueOperator
        """
        self.__event_queue = SynchronizedQueue()
        self.__enqueuer = __QueueOperator(self.__event_queue)
        
        self.__rshift__ = self.__enqueuer.__rshift__
        self.__lshift__ = self.__enqueuer.__lshift__
        
        self.__last_access_time = sys.current_time()
        
    def poll(self):
        """
        When called, one pending event will be resolved.
        Returns True if at least one event was resolved or False if queue is empty
        """
        dif = sys.current_time() - self.__last_access_time
        self.__last_access_time = sys.current_time() 
        with self.__event_queue.mutex:
            if self.__event_queue.has_elements():
                event = self.__event_queue.poll()
                event.currentPTime -= dif
                if event.currentPTime <= 0:
                    event.currentPTime = event.periodTime
                    self.fire_event(event)
                    if event.periodic:
                        self.__enqueuer.enqueue(event)
                    return True
                else:
                    self.__event_queue.add(event)
            return False
            
    def has_events(self):
        """
        Returns True if this reactor has events enqueued. 
        This might not be accurate value due to threading issues
        """
        with self.__event_queue.mutex:
            return len(self.__event_queue)
            
    def fire_event(self, event):
        """
        Base method, calls .call() on event. Is called when event needs to be fired by poll method
        """
        event.call()