forked from fluentpython/example-code
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimer2.py
More file actions
30 lines (26 loc) · 706 Bytes
/
timer2.py
File metadata and controls
30 lines (26 loc) · 706 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import asyncio
import sys
import contextlib
@asyncio.coroutine
def show_remaining(dots_task):
remaining = 5
while remaining:
print('Remaining: ', remaining)
sys.stdout.flush()
yield from asyncio.sleep(1)
remaining -= 1
dots_task.cancel()
print()
@asyncio.coroutine
def dots():
while True:
print('.', sep='', end='')
sys.stdout.flush()
yield from asyncio.sleep(.1)
def main():
with contextlib.closing(asyncio.get_event_loop()) as loop:
dots_task = asyncio.Task(dots())
coros = [show_remaining(dots_task), dots_task]
loop.run_until_complete(asyncio.wait(coros))
if __name__ == '__main__':
main()