Skip to content

Instantly share code, notes, and snippets.

@i2y
Created April 29, 2015 17:37
Show Gist options
  • Save i2y/14009b867206ded81c54 to your computer and use it in GitHub Desktop.
Save i2y/14009b867206ded81c54 to your computer and use it in GitHub Desktop.
RxPY example in Mochi
# usage: mochi -no-mp timer.mochi
# original:
# https://github.com/ReactiveX/RxPY/blob/master/examples/parallel/timer.py
import rx
import concurrent.futures
import time
seconds = [5, 1, 2, 4, 3]
def sleep(t):
time.sleep(t)
return t
def output(result):
print('%d seconds' % result)
with concurrent.futures.ProcessPoolExecutor(5) as executor:
rx.Observable.from_(seconds)
.flat_map((s) -> executor.submit(sleep, s))
.subscribe(output)
# 1 seconds
# 2 seconds
# 3 seconds
# 4 seconds
# 5 seconds
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment