8000 Two new examples: print 'Hello World' every two seconds, using a call… · Python-Repository-Hub/asyncio@276df9b · GitHub
[go: up one dir, main page]

Skip to content < 8000 link crossorigin="anonymous" media="all" rel="stylesheet" href="https://github.githubassets.com/assets/primer-react.8d5e42bdd3cd6a27871d.module.css" />

Commit 276df9b

Browse files
committed
Two new examples: print 'Hello World' every two seconds, using a callback and using a coroutine.
Thanks to Terry Reedy who suggested this exercise.
1 parent 6702599 commit 276df9b

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

examples/hello_callback.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
"""Print 'Hello World' every two seconds, using a callback."""
2+
3+
import asyncio
4+
5+
6+
def print_and_repeat(loop):
7+
print('Hello World')
8+
loop.call_later(2, print_and_repeat, loop)
9+
10+
11+
if __name__ == '__main__':
12+
loop = asyncio.get_event_loop()
13+
print_and_repeat(loop)
14+
loop.run_forever()

examples/hello_coroutine.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
"""Print 'Hello World' every two seconds, using a coroutine."""
2+
3+
import asyncio
4+
5+
6+
@asyncio.coroutine
7+
def greet_every_two_seconds():
8+
while True:
9+
print('Hello World')
10+
yield from asyncio.sleep(2)
11+
12+
13+
if __name__ == '__main__':
14+
loop = asyncio.get_event_loop()
15+
loop.run_until_complete(greet_every_two_seconds())

0 commit comments

Comments
 (0)
0