demos enhanced during workshop at Garoa

This commit is contained in:
Luciano Ramalho
2015-01-19 23:27:17 -02:00
parent fe0db0fa7b
commit c1e50e4477
6 changed files with 68 additions and 14 deletions

29
concurrency/timer_clo.py Normal file
View File

@@ -0,0 +1,29 @@
import sys
import asyncio
def make_show_remaining(seconds):
remaining = seconds
def show_remaining(loop):
nonlocal remaining
print('Remaining: ', remaining)
remaining -= 1
if remaining:
loop.call_later(1, show_remaining, loop)
else:
loop.stop()
return show_remaining
def main(seconds=5):
seconds = int(seconds)
loop = asyncio.get_event_loop()
try:
loop.call_soon(make_show_remaining(seconds), loop)
loop.run_forever()
finally:
loop.close()
if __name__ == '__main__':
main(*sys.argv[1:])