Created
June 15, 2015 23:57
-
-
Save nchammas/c1486678a0b36f38f22e to your computer and use it in GitHub Desktop.
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 shleepy_time(seconds): | |
print("Shleeping for {s} seconds...".format(s=seconds)) | |
yield from asyncio.sleep(seconds) | |
print("Done shleeping.") | |
@asyncio.coroutine | |
def raise_hell(): | |
raise Exception('hell') | |
if __name__ == '__main__': | |
loop = asyncio.get_event_loop() | |
# Side note: Apparently, async() will be deprecated in 3.4.4. | |
# See: https://docs.python.org/3.4/library/asyncio-task.html#asyncio.async | |
tasks = asyncio.gather( | |
asyncio.async(shleepy_time(seconds=2)), | |
asyncio.async(shleepy_time(seconds=5)), | |
asyncio.async(raise_hell()) | |
) | |
# return_exceptions=True) | |
try: | |
loop.run_until_complete(tasks) | |
except Exception as e: | |
print("Caught:", e) | |
tasks.cancel() | |
loop.run_forever() | |
tasks.exception() | |
finally: | |
loop.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This gist was posted to support a question I asked on Stack Overflow. You can see an explanation of why this code hangs in an infinite loop here.