Beispiel #1
0
    def on_transport(self, beacon):
        self.change_status(Garmin.Status.CONNECTED)

        while True:
            if self.timeout_source:
                self.timeout_source = None
                return

            while self.funcs:
                f, cb, args = self.funcs.pop(0)
                ret = f(self, *args)
                # run in ui thread
                GLib.idle_add(lambda: cb(ret))

            # we've run out of things to do for now. set a timer so we don't
            # disconnect immediately.

            # use a new context as we don't want to get mixed up with the
            # other mainloop currently running
            context = GLib.MainContext()
            self.loop = GLib.MainLoop(context)
            def timeout_cb(data=None):
                self.loop.quit()
                self.loop = None
            self.timeout_source = GLib.timeout_source_new_seconds(5)
            self.timeout_source.set_callback(timeout_cb)
            self.timeout_source.attach(context)
            self.loop.run()
Beispiel #2
0
    def on_transport(self, beacon):
        self.change_status(Garmin.Status.CONNECTED)

        while True:
            if self.timeout_source:
                self.timeout_source = None
                return

            while self.funcs:
                f, cb, args = self.funcs.pop(0)
                ret = f(self, *args)
                # run in ui thread
                GLib.idle_add(lambda: cb(ret))

            # we've run out of things to do for now. set a timer so we don't
            # disconnect immediately.

            # use a new context as we don't want to get mixed up with the
            # other mainloop currently running
            context = GLib.MainContext()
            self.loop = GLib.MainLoop(context)

            def timeout_cb(data=None):
                self.loop.quit()
                self.loop = None

            self.timeout_source = GLib.timeout_source_new_seconds(5)
            self.timeout_source.set_callback(timeout_cb)
            self.timeout_source.attach(context)
            self.loop.run()
Beispiel #3
0
 def run(timeout_seconds=5):
     source = GLib.timeout_source_new_seconds(timeout_seconds)
     source.set_callback(quit_cb)
     source.attach()
     GLib.MainLoop.run(mainloop)
     source.destroy()
     if timed_out:
         raise Exception("Timed out after %s seconds" % timeout_seconds)
Beispiel #4
0
 def run(timeout_seconds=5):
     source = GLib.timeout_source_new_seconds(timeout_seconds)
     source.set_callback(quit_cb)
     source.attach()
     GLib.MainLoop.run(mainloop)
     source.destroy()
     if timed_out:
         raise Exception("Timed out after %s seconds" % timeout_seconds)
Beispiel #5
0
 def run(timeout_seconds=5, until_empty=False):
     source = GLib.timeout_source_new_seconds(timeout_seconds)
     source.set_callback(timeout_cb)
     source.attach()
     if until_empty:
         GLib.idle_add(mainloop.quit)
     GLib.MainLoop.run(mainloop)
     source.destroy()
     if timed_out:
         raise Exception("Timed out after %s seconds" % timeout_seconds)
    def create_glib_loop(self):
        # clear flags
        self.timeout_error = False
        self.loop = GLibEventLoop()

        loop = self.loop.active_main_loop
        context = loop.get_context()

        # This is prevention from running loop indefinitely
        source = GLib.timeout_source_new_seconds(2)
        source.set_callback(self._quit_loop, loop)
        source.attach(context)
Beispiel #7
0
def timer(delay_sec, func, repeat=False):
    """
    Executes the given function after a delay given in seconds. Repeats every delay_sec if
    repeat==True. The function is executed in the context of the GLib MainContext thread.

    func is not called with any arguments, so to call with custom arguments use functools.partial,
    such as `timer(0.5, partial(myfunc, myarg1, myarg2))`.

    To cancel the timer, call `.cancel()` on the returned object.

    """
    frac, _ = math.modf(delay_sec)
    if frac == 0.0:
        source = GLib.timeout_source_new_seconds(delay_sec)
    else:
        source = GLib.timeout_source_new(delay_sec * 1000)

    return TimerContext(source, func, repeat)
Beispiel #8
0
 def createTimeout(self, interval, callback):
     logging.debug('Creating new timeout_source')
     self.timeoutSource = GLib.timeout_source_new_seconds(interval)  
     self.timeoutSource.set_callback(callback)
     self.timeoutSource.attach(None)
Beispiel #9
0
 def createTimeout(self, interval, callback):
     logging.debug('Creating new timeout_source')
     self.timeoutSource = GLib.timeout_source_new_seconds(interval)
     self.timeoutSource.set_callback(callback)
     self.timeoutSource.attach(None)