Skip to content

Instantly share code, notes, and snippets.

@willir
Last active November 8, 2015 13:44
Show Gist options
  • Save willir/4fde35b2a3b54ef93b37 to your computer and use it in GitHub Desktop.
Save willir/4fde35b2a3b54ef93b37 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import asyncio
import signal
def my_handler():
print('Stopping')
for task in asyncio.Task.all_tasks():
task.cancel()
@asyncio.coroutine
def do_some(some_args):
while True:
print("Do staff with %s" % some_args)
yield from asyncio.sleep(2)
def main():
loop = asyncio.get_event_loop()
loop.add_signal_handler(signal.SIGINT, my_handler)
try:
loop.run_until_complete(asyncio.wait([do_some(1), do_some(2)]))
except asyncio.CancelledError:
print('Tasks has been canceled')
finally:
loop.close()
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment