Skip to content

Instantly share code, notes, and snippets.

@keitheis
Forked from moskytw/parallel_with_rx.py
Last active June 1, 2016 03:10
Show Gist options
  • Save keitheis/304e703776a80dd07a43eaab9cacb767 to your computer and use it in GitHub Desktop.
Save keitheis/304e703776a80dd07a43eaab9cacb767 to your computer and use it in GitHub Desktop.
import time
import concurrent.futures
import rx
num_stream = [1, 2, 3, 4, 5]
def heavy_work(data):
time.sleep(1)
return data
with concurrent.futures.ProcessPoolExecutor(5) as executor:
rx.Observable.from_(num_stream) \
.flat_map(
lambda num: executor.submit(heavy_work, num)
).subscribe(print)
# $ time py3 parallel_with_rx.py
# 3
# 2
# 1
# 0
# 4
#
# real 0m1.231s
# user 0m0.196s
# sys 0m0.057s
# how:
#
# 1. https://github.com/ReactiveX/RxPY/blob/09ed65003d2e579753b7d0c257c5f5ac318076d9/rx/linq/observable/selectmany.py#L12
# 2. https://github.com/ReactiveX/RxPY/blob/3e44b48f84f851ab37bbffdd4725d41d88061ef2/rx/linq/observable/fromfuture.py#L29
#
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment