Пример #1
0
    def run(self):
        """Execute events until the queue is empty.

        When there is a positive delay until the first event, the
        delay function is called and the event is left in the queue;
        otherwise, the event is removed from the queue and executed
        (its action function is called, passing it the argument).  If
        the delay function returns prematurely, it is simply
        restarted.

        It is legal for both the delay function and the action
        function to to modify the queue or to raise an exception;
        exceptions are not caught but the scheduler's state remains
        well-defined so run() may be called again.

        A questionable hack is added to allow other threads to run:
        just after an event is executed, a delay of 0 is executed, to
        avoid monopolizing the CPU when other threads are also
        runnable.

        """
        q = self.queue
        while True:
            self.__preemption_condition.acquire()
            if not q:
                self.__preemption_condition.release()
                break

            time, _, action, argument = checked_event = q[0]
            now = timefunc()
            if now < time:
                self.__preemption_condition.wait(time - now)
                self.__preemption_condition.release()
            else:
                try:
                    event = heapq.heappop(q)
                    # Verify that the event was not removed or altered
                    # by another thread after we last looked at q[0].
                    if event == None or not isinstance(event, tuple):
                        pass
                    elif event is checked_event:
                        self.__preemption_condition.release()
                        try:
                            action(*argument)
                        except Exception:
                            self.__tracer.error(
                                ('Exception calling %s with args: \'%s\'.' +
                                 '\n\tDeleting scheduled event.')
                                 % (action, argument))

                            self.__tracer.debug(traceback.format_exc())

                        self.__preemption_condition.acquire()
                        del event
                    else:
                        heapq.heappush(q, event)
                finally:
                    self.__preemption_condition.release()
Пример #2
0
    def enter(self, delay, priority, action, argument):
        """A variant that specifies the time as a relative time.

        Identical to `enterabs`, except replacing `time` with
        `delay`. `delay` is the relative offset from the present to
        schedule `action`

        """
        time = timefunc() + delay
        return self.enterabs(time, priority, action, argument)
Пример #3
0
    def enter(self, delay, priority, action, argument):
        """A variant that specifies the time as a relative time.

        Identical to `enterabs`, except replacing `time` with
        `delay`. `delay` is the relative offset from the present to
        schedule `action`

        """
        time = timefunc() + delay
        return self.enterabs(time, priority, action, argument)
Пример #4
0
    def run(self):
        """Execute events until the queue is empty.

        When there is a positive delay until the first event, the
        delay function is called and the event is left in the queue;
        otherwise, the event is removed from the queue and executed
        (its action function is called, passing it the argument).  If
        the delay function returns prematurely, it is simply
        restarted.

        It is legal for both the delay function and the action
        function to to modify the queue or to raise an exception;
        exceptions are not caught but the scheduler's state remains
        well-defined so run() may be called again.

        A questionable hack is added to allow other threads to run:
        just after an event is executed, a delay of 0 is executed, to
        avoid monopolizing the CPU when other threads are also
        runnable.

        """
        q = self.queue
        while True:
            self.__preemption_condition.acquire()
            if not q:
                self.__preemption_condition.release()
                break

            time, priority, action, argument = checked_event = q[0]
            now = timefunc()
            if now < time:
                self.__preemption_condition.wait(time - now)
                self.__preemption_condition.release()
            else:
                try:
                    event = heapq.heappop(q)
                    # Verify that the event was not removed or altered
                    # by another thread after we last looked at q[0].
                    if event == None or not isinstance(event, tuple):
                        pass
                    elif event is checked_event:
                        self.__preemption_condition.release()
                        try:
                            action(*argument)
                        except Exception, e:
                            logger.error('exception during event "%s"' %
                                         str(e))
                            traceback.print_exc(file=sys.stdout)
                        self.__preemption_condition.acquire()
                        del event
                    else:
                        heapq.heappush(q, event)
Пример #5
0
    def run(self):
        """Execute events until the queue is empty.

        When there is a positive delay until the first event, the
        delay function is called and the event is left in the queue;
        otherwise, the event is removed from the queue and executed
        (its action function is called, passing it the argument).  If
        the delay function returns prematurely, it is simply
        restarted.

        It is legal for both the delay function and the action
        function to to modify the queue or to raise an exception;
        exceptions are not caught but the scheduler's state remains
        well-defined so run() may be called again.

        A questionable hack is added to allow other threads to run:
        just after an event is executed, a delay of 0 is executed, to
        avoid monopolizing the CPU when other threads are also
        runnable.

        """
        q = self.queue
        while True:
            self.__preemption_condition.acquire()
            if not q:
                self.__preemption_condition.release()
                break

            # we fetch the FIRST event, and since the events are queued in
            # future time order, this is the next even to run. Events after
            # it have either the SAME start time, or a later start time
            time, _, action, argument = checked_event = q[0]
            now = timefunc()
            if now < time:
                # then not yet time to run, this ends our cycle since all
                # events queued after this will also not be ready.
                self.__preemption_condition.wait(time - now)
                self.__preemption_condition.release()
            else:
                # else the scheduled time is OLDER than NOW
                try:
                    event = heapq.heappop(q)
                    # Verify that the event was not removed or altered
                    # by another thread after we last looked at q[0].
                    if event == None or not isinstance(event, tuple):
                        pass
                    elif event is checked_event:
                        self.__preemption_condition.release()
                        try:
                            action(*argument)
                        except Exception:
                            self.__tracer.error(
                                ('Exception calling %s with args: \'%s\'.' +
                                 '\n\tDeleting scheduled event.') %
                                (action, argument))

                            self.__tracer.debug(traceback.format_exc())

                        self.__preemption_condition.acquire()
                        del event
                    else:
                        heapq.heappush(q, event)
                finally:
                    self.__preemption_condition.release()