Skip to content

Instantly share code, notes, and snippets.

@nchammas
Created June 15, 2015 23:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nchammas/c1486678a0b36f38f22e to your computer and use it in GitHub Desktop.
Save nchammas/c1486678a0b36f38f22e to your computer and use it in GitHub Desktop.
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()
@nchammas
Copy link
Author

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.

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