Пример #1
0
    def test_immediate_scheduleaction(self):
        scheduler = ImmediateScheduler()
        ran = False

        def action(scheduler, state=None):
            nonlocal ran
            ran = True

        scheduler.schedule(action)
        assert ran
Пример #2
0
    def test_immediate_simple2(self):
        scheduler = ImmediateScheduler()
        xx = 0

        def action(scheduler, state=None):
            nonlocal xx
            xx = state
            return Disposable()

        scheduler.schedule_absolute(default_now(), action, 42)
        assert xx == 42
Пример #3
0
    def test_immediate_schedule_action_error(self):
        scheduler = ImmediateScheduler()

        class MyException(Exception):
            pass

        def action(scheduler, state=None):
            raise MyException()

        with pytest.raises(MyException):
            return scheduler.schedule(action)
Пример #4
0
    def test_immediate_simple1(self):
        scheduler = ImmediateScheduler()
        xx = 0

        def action(scheduler, state=None):
            nonlocal xx
            xx = state
            return Disposable()

        scheduler.schedule(action, 42)
        assert xx == 42
Пример #5
0
    def test_immediate_simple3(self):
        scheduler = ImmediateScheduler()
        xx = 0

        def action(scheduler, state=None):
            nonlocal xx
            xx = state
            return Disposable()

        scheduler.schedule_relative(DELTA_ZERO, action, 42)
        assert xx == 42
Пример #6
0
    def test_immediate_schedule_action_due_error(self):
        scheduler = ImmediateScheduler()
        ran = False

        def action(scheduler, state=None):
            nonlocal ran
            ran = True

        with pytest.raises(WouldBlockException):
            scheduler.schedule_relative(0.1, action)

        assert ran is False
Пример #7
0
 def test_immediate_now_units(self):
     scheduler = ImmediateScheduler()
     diff = scheduler.now
     sleep(1.1)
     diff = scheduler.now - diff
     assert timedelta(milliseconds=1000) < diff < timedelta(
         milliseconds=1300)
Пример #8
0
    def test_observe_on_forward_subscribe_scheduler(self):
        scheduler = ImmediateScheduler()
        expected_subscribe_scheduler = ImmediateScheduler()

        actual_subscribe_scheduler = None

        def subscribe(observer, scheduler):
            nonlocal actual_subscribe_scheduler
            actual_subscribe_scheduler = scheduler
            observer.on_completed()

        xs = rx.create(subscribe)

        xs.pipe(ops.observe_on(scheduler)).subscribe(
            scheduler=expected_subscribe_scheduler)

        assert expected_subscribe_scheduler == actual_subscribe_scheduler
Пример #9
0
    def test_immediate_singleton(self):
        scheduler = [ImmediateScheduler(), ImmediateScheduler.singleton()]
        assert scheduler[0] is scheduler[1]

        gate = [threading.Semaphore(0), threading.Semaphore(0)]
        scheduler = [None, None]

        def run(idx):
            scheduler[idx] = ImmediateScheduler()
            gate[idx].release()

        for idx in (0, 1):
            threading.Thread(target=run, args=(idx, )).start()
            gate[idx].acquire()

        assert scheduler[0] is not None
        assert scheduler[1] is not None
        assert scheduler[0] is scheduler[1]
Пример #10
0
    def subscribe(
            observer: typing.Observer,
            scheduler: Optional[typing.Scheduler] = None) -> typing.Disposable:
        _scheduler = scheduler or ImmediateScheduler.singleton()

        def action(scheduler: typing.Scheduler, state: Any):
            observer.on_error(exception)

        return _scheduler.schedule(action)
Пример #11
0
    def test_immediate_recursive1(self):
        scheduler = ImmediateScheduler()
        xx = 0
        yy = 0

        def action(scheduler, state=None):
            nonlocal xx
            xx = state

            def inner_action(scheduler, state=None):
                nonlocal yy
                yy = state
                return Disposable()

            return scheduler.schedule(inner_action, 43)

        scheduler.schedule(action, 42)
        assert xx == 42
        assert yy == 43
Пример #12
0
        def subscribe(observer, scheduler=None):
            scheduler = scheduler or ImmediateScheduler.singleton()

            queue = []
            m = SerialDisposable()
            d = CompositeDisposable(m)
            active_count = [0]
            is_acquired = [False]

            def ensure_active():
                is_owner = False
                if queue:
                    is_owner = not is_acquired[0]
                    is_acquired[0] = True

                def action(scheduler, state):
                    if queue:
                        work = queue.pop(0)
                    else:
                        is_acquired[0] = False
                        return

                    sad = SingleAssignmentDisposable()
                    d.add(sad)

                    def on_next(value):
                        observer.on_next(value)
                        result = None
                        try:
                            result = mapper(value)
                        except Exception as ex:
                            observer.on_error(ex)
                            return

                        queue.append(result)
                        active_count[0] += 1
                        ensure_active()

                    def on_complete():
                        d.remove(sad)
                        active_count[0] -= 1
                        if active_count[0] == 0:
                            observer.on_completed()

                    sad.disposable = work.subscribe_(on_next, observer.on_error, on_complete, scheduler)
                    m.disposable = scheduler.schedule(action)

                if is_owner:
                    m.disposable = scheduler.schedule(action)

            queue.append(source)
            active_count[0] += 1
            ensure_active()
            return d
Пример #13
0
 def __init__(self, protocol):
     # See comment in players.py.
     self._aio_scheduler = QtScheduler(QtCore)
     messages = protocol.register("game_info")
     self.new = messages.pipe(
         ops.flat_map(self._split_game_info),
         ops.observe_on(self._aio_scheduler),
         ops.flat_map(self._process_game),
         ops.observe_on(ImmediateScheduler()),
         ops.filter(lambda x: x is not None),
     )
Пример #14
0
    def subscribe(
            observer: typing.Observer,
            scheduler_: Optional[typing.Scheduler] = None
    ) -> typing.Disposable:

        _scheduler = scheduler or scheduler_ or ImmediateScheduler.singleton()

        def action(_: typing.Scheduler, __: Any) -> None:
            observer.on_completed()

        return _scheduler.schedule(action)
Пример #15
0
    def test_immediate_extend(self):
        class MyScheduler(ImmediateScheduler):
            pass

        scheduler = [
            MyScheduler(),
            MyScheduler.singleton(),
            ImmediateScheduler.singleton(),
        ]
        assert scheduler[0] is scheduler[1]
        assert scheduler[0] is not scheduler[2]
Пример #16
0
 def __init__(self, protocol):
     # We use this scheduler so that we don't handle all player messages at
     # the same time at startup, as this hangs the UI for a few seconds.
     # With this, it's just a minor slowdown.
     #
     # One side effect is that games and players don't appear immediately
     # (as in, UI is available before they're all shown). This shouldn't be
     # a problem.
     self._aio_scheduler = QtScheduler(QtCore)
     messages = protocol.register("player_info")
     self.new = messages.pipe(
         ops.flat_map(self._split_player_info),
         ops.observe_on(self._aio_scheduler),
         ops.flat_map(self._handle_player_info),
         ops.observe_on(ImmediateScheduler()),
     )
Пример #17
0
    def to_observable(self, scheduler=None):
        """Returns an observable sequence with a single notification,
        using the specified scheduler, else the immediate scheduler.

        Args:
            scheduler: [Optional] Scheduler to send out the
                notification calls on.

        Returns:
            An observable sequence that surfaces the behavior of the
            notification upon subscription.
        """

        scheduler = scheduler or ImmediateScheduler.singleton()

        def subscribe(observer, scheduler=None):
            def action(scheduler, state):
                self._accept_observer(observer)
                if self.kind == 'N':
                    observer.on_completed()

            return scheduler.schedule(action)

        return Observable(subscribe)
Пример #18
0
import os, glob
import rx
import rx.operators as op
from rx.scheduler import ImmediateScheduler

l = rx.from_iterable(glob.glob('./train/*.png')).pipe(
    op.map(os.path.basename), op.map(rx.just),
    op.flat_map(
        lambda x: x.pipe(
            op.repeat(3),
            op.subscribe_on(ImmediateScheduler()),
            op.map_indexed(lambda x, i: x.replace(".png", f"_{i}.png")),
        ), ), op.to_list()).run()

print(l)

l = rx.from_iterable(glob.glob('./train/*.png')).pipe(
    op.map(os.path.basename),
    op.map(rx.just),
    op.map(op.repeat(3)),
    op.map(op.subscribe_on(ImmediateScheduler())),
    # op.flat_map(lambda x : x.pipe(op.map_indexed(lambda x,i: x.replace(".png",f"_{i}.png")))),
    # op.flat_map(lambda x: op.map_indexed(lambda x,i: x.replace(".png",f"_{i}.png"))(x) ),
    # op.flat_map(op.map_indexed(lambda x,i: x.replace(".png",f"_{i}.png"))),
    # op.flat_map(lambda x: print('way',x) or x),
    op.map(lambda x: op.map_indexed(lambda x, i: x.replace(
        ".png", f"_{i}.png"))(x)),
    op.flat_map(lambda x: x),
    # op.flat_map_indexed(lambda x,i: print(x,i) or x),
    op.map(print)).run()
Пример #19
0
def deepspeech_server(aio_scheduler, sources):
    argv = sources.argv.argv
    stt = sources.httpd.route
    stt_response = sources.deepspeech.text
    ds_logs = sources.deepspeech.log

    http_ds_error, route_ds_error = make_error_router()

    args = parse_arguments(argv)

    read_request, read_response = args.pipe(
        ops.map(lambda i: file.Read(id='config', path=i.value)),
        file.read(sources.file.response),
    )
    read_request = read_request.pipe(
        ops.subscribe_on(aio_scheduler),
    )
    config = parse_config(read_response)

    logs_config = config.pipe(
        ops.flat_map(lambda i: rx.from_(i.log.level, scheduler=ImmediateScheduler())),
        ops.map(lambda i: logging.SetLevel(logger=i.logger, level=i.level)),
    )
    logs = rx.merge(logs_config, ds_logs)

    ds_stt = stt.pipe(
        ops.flat_map(lambda i: i.request),
        ops.map(lambda i: deepspeech.SpeechToText(data=i.data, context=i.context)),
    )

    # config is hot, the combine operator allows to keep its last value
    # until logging is initialized
    ds_arg = config.pipe(
        ops.map(lambda i: deepspeech.Initialize(
            model=i.deepspeech.model,
            scorer=deepspeech.Scorer(
                scorer=getattr(i.deepspeech, 'scorer', None),
                lm_alpha=getattr(i.deepspeech, 'lm_alpha', None),
                lm_beta=getattr(i.deepspeech, 'lm_beta', None),
            ),
            beam_width=getattr(i.deepspeech, 'beam_width', None),
        )),
    )
    ds = rx.merge(ds_stt, ds_arg)

    http_init = config.pipe(
        ops.flat_map(lambda i: rx.from_([
            httpd.Initialize(request_max_size=i.server.http.request_max_size),
            httpd.AddRoute(
                methods=['POST'],
                path='/stt',
                id='stt',
                headers=MultiDict([('Content-Type', 'text/plain')]),
            ),
            httpd.StartServer(
                host=i.server.http.host,
                port=i.server.http.port),
        ])),
    )

    http_response = stt_response.pipe(
        route_ds_error(
            error_map=lambda e: httpd.Response(
                data="Speech to text error".encode('utf-8'),
                context=e.args[0].context,
                status=500
        )),
        ops.map(lambda i: httpd.Response(
            data=i.text.encode('utf-8'),
            context=i.context,
        )),
    )

    http = rx.merge(http_init, http_response, http_ds_error)

    return DeepspeechSink(
        file=file.Sink(request=read_request),
        logging=logging.Sink(request=logs),
        deepspeech=deepspeech.Sink(speech=ds),
        httpd=httpd.Sink(control=http)
    )
Пример #20
0
 def test_immediate_now(self):
     scheduler = ImmediateScheduler()
     diff = scheduler.now - default_now()
     assert abs(diff) <= timedelta(milliseconds=1)
Пример #21
0
 def run(idx):
     scheduler[idx] = ImmediateScheduler()
     gate[idx].release()