ReactiveX / RxPY

ReactiveX for Python

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

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

RxPY/ example / timer.py broken: Future is not iterable

cheind opened this issue · comments

Describe the bug

Maybe I'm not seeing something, but I believe the example

https://github.com/ReactiveX/RxPY/blob/master/examples/parallel/timer.py

is broken for RxPY 4.0.4. Instead of printing the output it prints nothing, which is due to the on_error handler being called. Once hooked it states that Future is not iterable.

To Reproduce

python timer.py

Expected behavior

See output

1 seconds
2 seconds
3 seconds
4 seconds
5 seconds 

Fix

I believe the correct code should read

   with concurrent.futures.ProcessPoolExecutor(5) as executor:
        reactivex.from_(seconds).pipe(
            ops.flat_map(lambda s: reactivex.from_future(executor.submit(sleep, s)))
        ).subscribe(output)

Additional context

  • Linux
  • RxPY 4.0.4
  • Python 3.11

Question
Why is flat_map used in this context. Does it enable parallel features in some way compared to map?