import reactivex from reactivex import operators as ops """ Specify the error to be raised in place of the # symbol. """ err = ValueError("I don't like 5!") src0 = reactivex.from_marbles("12-----4-----67--|", timespan=0.2) src1 = reactivex.from_marbles("----3----5-# ", timespan=0.2, error=err) source = reactivex.merge(src0, src1).pipe(ops.do_action(print)) source.run()
def merge(source: Observable[Observable[_T]]) -> Observable[_T]: """Merges an observable sequence of observable sequences into an observable sequence, limiting the number of concurrent subscriptions to inner sequences. Or merges two observable sequences into a single observable sequence. Examples: >>> res = merge(sources) Args: source: Source observable. Returns: The observable sequence that merges the elements of the inner sequences. """ if max_concurrent is None: sources_ = tuple([source]) + sources return reactivex.merge(*sources_) def subscribe( observer: abc.ObserverBase[_T], scheduler: Optional[abc.SchedulerBase] = None, ): active_count = [0] group = CompositeDisposable() is_stopped = [False] queue: List[Observable[_T]] = [] def subscribe(xs: Observable[_T]): subscription = SingleAssignmentDisposable() group.add(subscription) @synchronized(source.lock) def on_completed(): group.remove(subscription) if queue: s = queue.pop(0) subscribe(s) else: active_count[0] -= 1 if is_stopped[0] and active_count[0] == 0: observer.on_completed() on_next = synchronized(source.lock)(observer.on_next) on_error = synchronized(source.lock)(observer.on_error) subscription.disposable = xs.subscribe(on_next, on_error, on_completed, scheduler=scheduler) def on_next(inner_source: Observable[_T]) -> None: assert max_concurrent if active_count[0] < max_concurrent: active_count[0] += 1 subscribe(inner_source) else: queue.append(inner_source) def on_completed(): is_stopped[0] = True if active_count[0] == 0: observer.on_completed() group.add( source.subscribe(on_next, observer.on_error, on_completed, scheduler=scheduler)) return group return Observable(subscribe)
import reactivex import reactivex.operators as ops """ Use a dictionnary to convert elements declared in the marbles diagram to the specified values. """ lookup0 = {"a": 1, "b": 3, "c": 5} lookup1 = {"x": 2, "y": 4, "z": 6} source0 = reactivex.cold("a---b----c----|", timespan=0.01, lookup=lookup0) source1 = reactivex.cold("---x---y---z--|", timespan=0.01, lookup=lookup1) observable = reactivex.merge(source0, source1).pipe(ops.to_iterable()) elements = observable.run() print("received {}".format(list(elements)))
import reactivex from reactivex import operators as ops source0 = reactivex.cold("a-----d---1--------4-|", timespan=0.1) source1 = reactivex.cold("--b-c-------2---3-| ", timespan=0.1) print("to_marbles() is a blocking operator, we need to wait for completion...") print('expecting "a-b-c-d---1-2---3--4-|"') observable = reactivex.merge(source0, source1).pipe(ops.to_marbles(timespan=0.1)) diagram = observable.run() print('got "{}"'.format(diagram))
def create(): return reactivex.merge(e1, e2, e3)
def create(): return reactivex.merge(n1, n2, n3)
def create(): return reactivex.merge(o1, o2)
def create(): return reactivex.merge(r1, e1)
def create(): return reactivex.merge(e1, r1)