Exemplo n.º 1
0
    def poll(self, timeout=None):
        '''Polls for events while handling timers.

        poll() will wait up to timeout seconds for sockets or files
        registered with self.reactor to become ready.  A timeout of None
        will cause poll to wait an infinite amount of time.  While
        waiting for poll events, scheduled events will be handled,
        potentially causing the wait time to slip a bit.
        '''
        elapsed = 0.0
        mono_time = clock.monotonic()
        while True:
            wall_time = time_mod.time()
            self._mono.execute(mono_time)
            self._wall.execute(wall_time)
            delays = [self.LOOP_INTERVAL if timeout is None
                      else min(timeout - elapsed, self.LOOP_INTERVAL),
                      self._mono.delay(mono_time), self._wall.delay(wall_time)]
            delay = min(d for d in delays if d is not None)
            events = self.reactor.poll(delay)
            if events:
                return events
            last_time, mono_time = mono_time, clock.monotonic()
            elapsed += mono_time - last_time
            if timeout is not None and elapsed >= timeout:
                return []
Exemplo n.º 2
0
    def poll(self, timeout=None):
        '''Polls for events while handling timers.

        poll() will wait up to timeout seconds for sockets or files
        registered with self.reactor to become ready.  A timeout of None
        will cause poll to wait an infinite amount of time.  While
        waiting for poll events, scheduled events will be handled,
        potentially causing the wait time to slip a bit.
        '''
        elapsed = 0.0
        mono_time = clock.monotonic()
        while True:
            wall_time = time_mod.time()
            self._mono.execute(mono_time)
            self._wall.execute(wall_time)
            delays = [
                self.LOOP_INTERVAL if timeout is None else min(
                    timeout - elapsed, self.LOOP_INTERVAL),
                self._mono.delay(mono_time),
                self._wall.delay(wall_time)
            ]
            delay = min(d for d in delays if d is not None)
            events = self.reactor.poll(delay)
            if events:
                return events
            last_time, mono_time = mono_time, clock.monotonic()
            elapsed += mono_time - last_time
            if timeout is not None and elapsed >= timeout:
                return []
Exemplo n.º 3
0
    def periodic_timer(self, period, function, *args, **kwargs):
        '''Create a periodic timer to call function every period seconds.

        Like the timer method except that the timer is automatically
        rearmed after the function completes.
        '''
        timer = sched.RecurringEvent(period, function, args, kwargs)
        self._mono.schedule(clock.monotonic() + period, timer)
        return timer
Exemplo n.º 4
0
    def periodic_timer(self, period, function, *args, **kwargs):
        '''Create a periodic timer to call function every period seconds.

        Like the timer method except that the timer is automatically
        rearmed after the function completes.
        '''
        timer = sched.RecurringEvent(period, function, args, kwargs)
        self._mono.schedule(clock.monotonic() + period, timer)
        return timer
Exemplo n.º 5
0
    def timer(self, interval, function, *args, **kwargs):
        '''Create a timer to call function after interval seconds.

        interval is specified in seconds and can include fractional part.
        function is a function that takes the optional args and kwargs.
        Returns a timer object that can be used to modify the callback
        parameters or to cancel using the cancel() method.
        '''
        timer = sched.Event(function, args, kwargs)
        self._mono.schedule(clock.monotonic() + interval, timer)
        return timer
Exemplo n.º 6
0
    def timer(self, interval, function, *args, **kwargs):
        '''Create a timer to call function after interval seconds.

        interval is specified in seconds and can include fractional part.
        function is a function that takes the optional args and kwargs.
        Returns a timer object that can be used to modify the callback
        parameters or to cancel using the cancel() method.
        '''
        timer = sched.Event(function, args, kwargs)
        self._mono.schedule(clock.monotonic() + interval, timer)
        return timer
Exemplo n.º 7
0
 def ping_back(self, callback, timeout=None, period=1):
     if timeout is not None:
         start = clock.monotonic()
     ping = topics.AGENT_PING(cookie=random_cookie())
     state = {}
     def finish(success):
         state['timer'].cancel()
         self.unsubscribe(state['subscription'])
         callback(success)
     def send_ping():
         if timeout is not None:
             if (clock.monotonic() - start) >= timeout:
                 finish(False)
         self.publish(ping, {})
     def on_ping(topic, headers, msg, match):
         finish(True)
     state['subscription'] = self.subscribe(ping, on_ping, None)
     state['timer'] = self.periodic_timer(period, send_ping)
     send_ping()
Exemplo n.º 8
0
    def ping_back(self, callback, timeout=None, period=1):
        if timeout is not None:
            start = clock.monotonic()
        ping = topics.AGENT_PING(cookie=random_cookie())
        state = {}

        def finish(success):
            state['timer'].cancel()
            self.unsubscribe(state['subscription'])
            callback(success)

        def send_ping():
            if timeout is not None:
                if (clock.monotonic() - start) >= timeout:
                    finish(False)
            self.publish(ping, {})

        def on_ping(topic, headers, msg, match):
            finish(True)

        state['subscription'] = self.subscribe(ping, on_ping, None)
        state['timer'] = self.periodic_timer(period, send_ping)
        send_ping()
Exemplo n.º 9
0
 def send_ping():
     if timeout is not None:
         if (clock.monotonic() - start) >= timeout:
             finish(False)
     self.publish(ping, {})
Exemplo n.º 10
0
 def send_ping():
     if timeout is not None:
         if (clock.monotonic() - start) >= timeout:
             finish(False)
     self.publish(ping, {})