Skip to content

Instantly share code, notes, and snippets.

@moskytw
Last active June 1, 2016 02:56
Show Gist options
  • Save moskytw/92e7d918c0b91518491c0eecaac46347 to your computer and use it in GitHub Desktop.
Save moskytw/92e7d918c0b91518491c0eecaac46347 to your computer and use it in GitHub Desktop.
import time
import concurrent.futures
import rx
def return_slowly(i):
time.sleep(1)
return i
with concurrent.futures.ProcessPoolExecutor(5) as executor:
rx.Observable.from_(range(5)).flat_map(
lambda i: executor.submit(return_slowly, i)
).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