Esempio n. 1
0
    def schedule_periodic(self, period, action, state=None):
        """Schedules a periodic piece of work to be executed in the tkinter
        mainloop.

        Keyword arguments:
        period -- Period in milliseconds for running the work periodically.
        action -- Action to be executed.
        state -- [Optional] Initial state passed to the action upon the first
            iteration.

        Returns the disposable object used to cancel the scheduled recurring
        action (best effort)."""

        disposable = MultipleAssignmentDisposable()
        state = [state]

        def invoke_action(scheduler, _):
            if disposable.is_disposed:
                return

            new_state = action(state[0])
            if new_state is not None:
                state[0] = new_state
            disposable.disposable = self.schedule_relative(period, invoke_action, state)

        disposable.disposable = self.schedule_relative(period, invoke_action, state)
        return disposable
Esempio n. 2
0
    def subscribe(observer):
        first = [True]
        state = [initial_state]
        mad = MultipleAssignmentDisposable()

        def action(scheduler, state1=None):
            has_result = False
            result = None

            try:
                if first[0]:
                    first[0] = False
                else:
                    state[0] = iterate(state[0])

                has_result = condition(state[0])
                if has_result:
                    result = result_selector(state[0])

            except Exception as exception:
                observer.on_error(exception)
                return

            if has_result:
                observer.on_next(result)
                mad.disposable = scheduler.schedule(action)
            else:
                observer.on_completed()

        mad.disposable = scheduler.schedule(action)
        return mad
    def subscribe(observer):
        mad = MultipleAssignmentDisposable()
        state = [initial_state]
        has_result = [False]
        result = [None]
        first = [True]
        time = [None]

        def action(scheduler, _):
            if has_result[0]:
                observer.on_next(result[0])

            try:
                if first[0]:
                    first[0] = False
                else:
                    state[0] = iterate(state[0])

                has_result[0] = condition(state[0])
                if has_result[0]:
                    result[0] = result_selector(state[0])
                    time[0] = time_selector(state[0])

            except Exception as e:
                observer.on_error(e)
                return

            if has_result[0]:
                mad.disposable = scheduler.schedule_relative(time[0], action)
            else:
                observer.on_completed()

        mad.disposable = scheduler.schedule_relative(0, action)
        return mad
Esempio n. 4
0
    def subscribe(observer):
        first = [True]
        state = [initial_state]
        mad = MultipleAssignmentDisposable()

        def action(scheduler, state1=None):
            has_result = False
            result = None

            try:
                if first[0]:
                    first[0] = False
                else:
                    state[0] = iterate(state[0])

                has_result = condition(state[0])
                if has_result:
                    result = result_selector(state[0])

            except Exception as exception:
                observer.on_error(exception)
                return

            if has_result:
                observer.on_next(result)
                mad.disposable = scheduler.schedule(action)
            else:
                observer.on_completed()

        mad.disposable = scheduler.schedule(action)
        return mad
Esempio n. 5
0
        def on_next(notification):
            log.debug("observable_delay_timespan:subscribe:on_next()")
            should_run = False

            with source.lock:
                if notification.value.kind == 'E':
                    del queue[:]
                    queue.append(notification)
                    exception[0] = notification.value.exception
                    should_run = not running[0]
                else:
                    queue.append(Timestamp(value=notification.value, timestamp=notification.timestamp + duetime))
                    should_run = not active[0]
                    active[0] = True

            if should_run:
                if exception[0]:
                    log.error("*** Exception: %s", exception[0])
                    observer.on_error(exception[0])
                else:
                    mad = MultipleAssignmentDisposable()
                    cancelable.disposable = mad

                    def action(scheduler, state):
                        if exception[0]:
                            log.error("observable_delay_timespan:subscribe:on_next:action(), exception: %s", exception[0])
                            return

                        with source.lock:
                            running[0] = True
                            while True:
                                result = None
                                if len(queue) and queue[0].timestamp <= scheduler.now:
                                    result = queue.pop(0).value

                                if result:
                                    result.accept(observer)

                                if not result:
                                    break

                            should_continue = False
                            recurse_duetime = 0
                            if len(queue):
                                should_continue = True
                                diff = queue[0].timestamp - scheduler.now
                                zero = timedelta(0) if isinstance(diff, timedelta) else 0
                                recurse_duetime = max(zero, diff)
                            else:
                                active[0] = False

                            ex = exception[0]
                            running[0] = False

                        if ex:
                            observer.on_error(ex)
                        elif should_continue:
                            mad.disposable = scheduler.schedule_relative(recurse_duetime, action)

                    mad.disposable = scheduler.schedule_relative(duetime, action)
Esempio n. 6
0
    def schedule_periodic(self, period, action, state=None):
        """Schedules a periodic piece of work to be executed in the tkinter
        mainloop.

        Keyword arguments:
        period -- Period in milliseconds for running the work periodically.
        action -- Action to be executed.
        state -- [Optional] Initial state passed to the action upon the first
            iteration.

        Returns the disposable object used to cancel the scheduled recurring
        action (best effort)."""

        disposable = MultipleAssignmentDisposable()
        state = [state]

        def invoke_action(scheduler, _):
            if disposable.is_disposed:
                return

            new_state = action(state[0])
            if new_state is not None:
                state[0] = new_state
            disposable.disposable = self.schedule_relative(period, invoke_action, state)

        disposable.disposable = self.schedule_relative(period, invoke_action, state)
        return disposable
Esempio n. 7
0
    def subscribe(observer):
        mad = MultipleAssignmentDisposable()
        state = [initial_state]
        has_result = [False]
        result = [None]
        first = [True]
        time = [None]

        def action(scheduler, _):
            if has_result[0]:
                observer.on_next(result[0])

            try:
                if first[0]:
                    first[0] = False
                else:
                    state[0] = iterate(state[0])

                has_result[0] = condition(state[0])
                if has_result[0]:
                    result[0] = result_selector(state[0])
                    time[0] = time_selector(state[0])

            except Exception as e:
                observer.on_error(e)
                return

            if has_result[0]:
                mad.disposable = scheduler.schedule_relative(time[0], action)
            else:
                observer.on_completed()

        mad.disposable = scheduler.schedule_relative(0, action)
        return mad
Esempio n. 8
0
    def subscribe(observer):
        sd = MultipleAssignmentDisposable()

        def action(scheduler, n):
            if n < end:
                observer.on_next(n)
                sd.disposable = scheduler.schedule(action, n + 1)
            else:
                observer.on_completed()

        sd.disposable = scheduler.schedule(action, start)
        return sd
Esempio n. 9
0
    def subscribe(observer):
        sd = MultipleAssignmentDisposable()

        def action(scheduler, n):
            if n < end:
                observer.on_next(n)
                sd.disposable = scheduler.schedule(action, n + 1)
            else:
                observer.on_completed()

        sd.disposable = scheduler.schedule(action, start)
        return sd
Esempio n. 10
0
    def subscribe(observer, scheduler=None):
        nonlocal range_t

        scheduler = scheduler or current_thread_scheduler
        sd = MultipleAssignmentDisposable()

        def action(scheduler, iterator):
            try:
                observer.on_next(next(iterator))
                sd.disposable = scheduler.schedule(action, state=iterator)
            except StopIteration:
                observer.on_completed()

        sd.disposable = scheduler.schedule(action, iter(range_t))
        return sd
Esempio n. 11
0
    def subscribe(observer):
        sd = MultipleAssignmentDisposable()
        iterator = iter(iterable)

        def action(scheduler, state=None):
            try:
                with lock:
                    item = next(iterator)

            except StopIteration:
                observer.on_completed()
            else:
                observer.on_next(item)
                sd.disposable = scheduler.schedule(action)

        sd.disposable = scheduler.schedule(action)
        return sd
Esempio n. 12
0
    def subscribe(observer):
        mad = MultipleAssignmentDisposable()
        dt = [duetime]
        count = [0]

        def action(scheduler, state):
            if p > 0:
                now = scheduler.now
                dt[0] = dt[0] + scheduler.to_timedelta(p)
                if dt[0] <= now:
                    dt[0] = now + scheduler.to_timedelta(p)

            observer.on_next(count[0])
            count[0] += 1
            mad.disposable = scheduler.schedule_absolute(dt[0], action)
        mad.disposable = scheduler.schedule_absolute(dt[0], action)
        return mad
Esempio n. 13
0
    def subscribe(observer):
        sd = MultipleAssignmentDisposable()
        iterator = iter(iterable)

        def action(scheduler, state=None):
            try:
                with lock:
                    item = next(iterator)

            except StopIteration:
                observer.on_completed()
            else:
                observer.on_next(item)
                sd.disposable = scheduler.schedule(action)

        sd.disposable = scheduler.schedule(action)
        return sd
Esempio n. 14
0
    def subscribe(observer):
        mad = MultipleAssignmentDisposable()
        dt = [duetime]
        count = [0]

        def action(scheduler, state):
            if p > 0:
                now = scheduler.now
                dt[0] = dt[0] + scheduler.to_timedelta(p)
                if dt[0] <= now:
                    dt[0] = now + scheduler.to_timedelta(p)

            observer.on_next(count[0])
            count[0] += 1
            mad.disposable = scheduler.schedule_absolute(dt[0], action)

        mad.disposable = scheduler.schedule_absolute(dt[0], action)
        return mad
Esempio n. 15
0
    def subscribe(observer, scheduler=None):
        nonlocal duetime

        if isinstance(duetime, int):
            duetime = scheduler.now + scheduler.to_timedelta(duetime)

        p = scheduler.normalize(period)
        mad = MultipleAssignmentDisposable()
        dt = [duetime]
        count = [0]

        def action(scheduler, state):
            if p > 0:
                now = scheduler.now
                dt[0] = dt[0] + scheduler.to_timedelta(p)
                if dt[0] <= now:
                    dt[0] = now + scheduler.to_timedelta(p)

            observer.on_next(count[0])
            count[0] += 1
            mad.disposable = scheduler.schedule_absolute(dt[0], action)

        mad.disposable = scheduler.schedule_absolute(dt[0], action)
        return mad
Esempio n. 16
0
        def on_next(notification):
            log.debug("observable_delay_timespan:subscribe:on_next()")
            should_run = False

            with source.lock:
                if notification.value.kind == 'E':
                    del queue[:]
                    queue.append(notification)
                    exception[0] = notification.value.exception
                    should_run = not running[0]
                else:
                    queue.append(
                        Timestamp(value=notification.value,
                                  timestamp=notification.timestamp + duetime))
                    should_run = not active[0]
                    active[0] = True

            if should_run:
                if exception[0]:
                    log.error("*** Exception: %s", exception[0])
                    observer.on_error(exception[0])
                else:
                    mad = MultipleAssignmentDisposable()
                    cancelable.disposable = mad

                    def action(scheduler, state):
                        if exception[0]:
                            log.error(
                                "observable_delay_timespan:subscribe:on_next:action(), exception: %s",
                                exception[0])
                            return

                        with source.lock:
                            running[0] = True
                            while True:
                                result = None
                                if len(queue) and queue[
                                        0].timestamp <= scheduler.now:
                                    result = queue.pop(0).value

                                if result:
                                    result.accept(observer)

                                if not result:
                                    break

                            should_continue = False
                            recurse_duetime = 0
                            if len(queue):
                                should_continue = True
                                diff = queue[0].timestamp - scheduler.now
                                zero = timedelta(0) if isinstance(
                                    diff, timedelta) else 0
                                recurse_duetime = max(zero, diff)
                            else:
                                active[0] = False

                            ex = exception[0]
                            running[0] = False

                        if ex:
                            observer.on_error(ex)
                        elif should_continue:
                            mad.disposable = scheduler.schedule_relative(
                                recurse_duetime, action)

                    mad.disposable = scheduler.schedule_relative(
                        duetime, action)