Skip to content

Instantly share code, notes, and snippets.

@agrif
Created September 23, 2012 01:01
Show Gist options
  • Save agrif/3768411 to your computer and use it in GitHub Desktop.
Save agrif/3768411 to your computer and use it in GitHub Desktop.
# 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)
@agrif
Copy link
Author

agrif commented Sep 23, 2012

def get_names(ircplugin):
    yield from ircplugin.send("NAMES") # or whatever
    names = []
    while 1:
        yield until(lambda: ircplugin.last_message.startswith("NAMES-RESPONSE"))
        # ... parse out names, add them onto `names` ...
        if message_is_last:
            break
    return names

## usage
names = yield from get_names(ircplugin)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment