timer examples
This commit is contained in:
23
concurrency/timer_cb.py
Normal file
23
concurrency/timer_cb.py
Normal file
@@ -0,0 +1,23 @@
|
||||
import asyncio
|
||||
|
||||
def show_remaining(loop):
|
||||
if not hasattr(show_remaining, 'remaining'):
|
||||
show_remaining.remaining = 5
|
||||
|
||||
print('Remaining: ', show_remaining.remaining)
|
||||
show_remaining.remaining -= 1
|
||||
if show_remaining.remaining:
|
||||
loop.call_later(1, show_remaining, loop)
|
||||
else:
|
||||
loop.stop()
|
||||
|
||||
def main():
|
||||
loop = asyncio.get_event_loop()
|
||||
try:
|
||||
loop.call_soon(show_remaining, loop)
|
||||
loop.run_forever()
|
||||
finally:
|
||||
loop.close()
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
Reference in New Issue
Block a user