I am currently trying to learn to use RxPy but run into the following. I tried the use of both rx.timer
and rx.interval
but both produce no output.
My code:
print("python version = {}".format(sys.version)) # 3.8.10
print("rx version = {}".format(rx.__version__)) # 3.2.0
rx.timer(1.0, 0.5).subscribe(
on_next=lambda i: print("tick {}".format(i)),
on_error=lambda e: print("error: {}".format(e)),
on_completed=lambda: print("completed")
)
print("try something else...")
rx.interval(1.0).subscribe(
on_next=lambda i: print("tick {}".format(i)),
on_error=lambda e: print("error: {}".format(e)),
on_completed=lambda: print("completed")
)
print("try something completely else...")
rx.interval(.01).subscribe(on_next=lambda i: print(i))
which produces this result:
python version = 3.8.10 (default, Sep 28 2021, 16:10:42)
rx version = 3.2.0
try something else...
try something completely else...
What am I missing here?
rx.interval
and rx.timer
only produce results if the code runs long enough for the corresponding time intervals to elapse.
In your example, you set up the timers, but then the code immediately finishes, so you never get to see even the first result.
In the simplest case, call time.sleep
at the end to let the code run for several more seconds and observe the outputs. Of course, if your program will eventually do something useful instead, adding an artificial waiting time is not necessary.