1
from rx import Observable, Observer
from __future__ import print_function
import random

def create_observable(observer):
    while True:
        observer.on_next(random.randint(1,100))    

Observable.create(create_observable).take_while(lambda x: x>50).repeat(6).subscribe(print)

gives

74 78 94 59 79 76

sequence, while I'm expecting each number will be repeated 6 times

so "repeat" never works for observables created with create method.

Andrew Matiuk
  • 924
  • 1
  • 7
  • 21

2 Answers2

1

The posted code gets 6 times a sequence of random integers in the range [51, 100].

Try with

(Observable.create(create_observable)
    .take_while(lambda x: x > 50)
    .select_many(lambda x: Observable.just(x).repeat(6))
    .subscribe(print))

or just

(Observable.create(create_observable)
    .take_while(lambda x: x > 50)
    .select_many(lambda v: [v] * 6)
    .subscribe(print))
mkzqeyns
  • 71
  • 2
0

The problem is with infinite loop in your create function. Whenever you subscribe such observable it calls function passed to create, and there is no way to exit this loop. Your program gets stuck at this point, no further code executes. You did not mention, but I suppose you had to forcefully terminate it.

When generating infinite sequencies you have to check if subscription is alive after each iteration. The easiest and safest way to do this is by using Observable.generate() function:

Observable.generate(random.randint(1, 100), 
                    lambda x: True,
                    lambda x: random.randint(1, 100),
                    lambda x: x) \
    .take_while(lambda x: x > 50) \
    .repeat(6) \
    .subscribe(print)
Yaroslav Stavnichiy
  • 20,738
  • 6
  • 52
  • 55
  • your code prints – Andrew Matiuk Jan 14 '17 at 04:42
  • @AndrewMatuk Disposable is what `subscribe` method returns. I put wrong initial value into generator, which `take_while` did not pass, so there were no generated values. I have fixed the sample, and tried it myself. There seems to be a bug in RxPY: https://github.com/ReactiveX/RxPY/issues/157 – Yaroslav Stavnichiy Jan 14 '17 at 11:55