ReactiveX / RxPY

ReactiveX for Python

Home Page:https://rxpy.rtfd.io

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to use interval and timer

MikeDabrowski opened this issue · comments

Hi, its my second time trying this library and after half a year not much has changed with the interval and timer. Basicly they do not work as supposed.

from rx import scheduler, interval

interval(0.1, scheduler = scheduler.TimeoutScheduler()).subscribe(on_next=lambda i: print(i))
return None

/// output:
Process finished with exit code 0

Not passing and passing the scheduler seems to have no additional effect. Both timer and interval do not emit any values and just die for no apparent reason.

Hi @MikeDabrowski, by my understanding there needs to be some work after you have called subscribe on the observable for it to actually do the work This is because the work is scheduled on another thread, and not the one your script is running on. In your case, it just subscribes and exits the script, not having got a chance to do "real" work. A simple example of keeping the script alive would be:

from rx import scheduler, interval

interval(0.1, scheduler=scheduler.TimeoutScheduler()).subscribe(
    on_next=lambda i: print(f"Got {i}")
)

input("Keep the script alive...\n")

Wow. You are right. I haven't even thought about it ...

I should also point out that input was just for the sake of the example. In real code, you might use threading.Event or have an main loop somewhere.

That one I figured. Thanks.
python is a hobby language for me and I miss quite a lot of basics 😅