-
-
Save 0x3bfc/07bb92ad8773fa221f5b0fe92fa5c330 to your computer and use it in GitHub Desktop.
asyncio examples
This file contains 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
import asyncio | |
@asyncio.coroutine | |
def waiting(r): | |
print("hello from waiting -", r) | |
yield from asyncio.sleep(2) | |
print("bye from waiting -", r) | |
return r | |
@asyncio.coroutine | |
def serial(): | |
a = yield from waiting(1) | |
b = yield from waiting(2) | |
c = yield from waiting(a + b) | |
print(c) | |
@asyncio.coroutine | |
def parallel(): | |
a, b = yield from asyncio.gather(waiting(1), waiting(2)) | |
c = yield from waiting(a + b) | |
print(c) | |
@asyncio.coroutine | |
def postponed(): | |
a = yield from waiting(1) | |
b = yield from waiting(2) | |
asyncio.Task(waiting('after')) | |
return a + b | |
@asyncio.coroutine | |
def pp_caller(): | |
r = yield from postponed() | |
print("got result from postponed -", r) | |
yield from asyncio.sleep(3) | |
loop.stop() | |
loop = asyncio.get_event_loop() | |
print("-- serial") | |
loop.run_until_complete(serial()) | |
print("-- parallel") | |
loop.run_until_complete(parallel()) | |
print("-- postponed") | |
asyncio.Task(pp_caller()) | |
loop.run_forever() | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment