Created
September 23, 2012 01:01
-
-
Save agrif/3768411 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# a silly long computation | |
def long_computation(a, b): | |
print("doing long computation", a, b) | |
# sleep for 2 seconds, asynchronously | |
yield until_elapsed(2) | |
print("computation successfull", a, b) | |
return a + b | |
def many_long_computations(a, b, c, d): | |
# run long_computation asynchronously, store return val in res1 | |
res1 = yield from long_computation(a, b) | |
print("result 1", res1) | |
# similar to above, but will always occur after above | |
res2 = yield from long_computation(c, d) | |
print("result 2", res2) | |
# run two many_long_computations instances in parallel | |
e = Engine() | |
e.add(many_long_computations(1, 2, 3, 4)) | |
e.add(many_long_computations(5, 6, 7, 8)) | |
e.run() | |
# runs in 4 seconds total | |
# each .add()'d iterator runs in parallel (in only one thread total) |
Author
agrif
commented
Sep 23, 2012
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment