diff options
author | Guido van Rossum <guido@python.org> | 2013-11-22 20:32:19 -0800 |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2013-11-22 20:32:19 -0800 |
commit | 0cad894f03ab26b9991df2d4ff054ff51683fe31 (patch) | |
tree | 68a5ce4113b337a8992b81876896e88434a19aea /examples | |
parent | 5935163391ece406d52b01485957a94cb50cad67 (diff) | |
download | trollius-0cad894f03ab26b9991df2d4ff054ff51683fe31.tar.gz |
Two new examples: print 'Hello World' every two seconds, using a callback and using a coroutine.
Thanks to Terry Reedy who suggested this exercise.
Diffstat (limited to 'examples')
-rw-r--r-- | examples/hello_callback.py | 14 | ||||
-rw-r--r-- | examples/hello_coroutine.py | 15 |
2 files changed, 29 insertions, 0 deletions
diff --git a/examples/hello_callback.py b/examples/hello_callback.py new file mode 100644 index 0000000..df889e5 --- /dev/null +++ b/examples/hello_callback.py @@ -0,0 +1,14 @@ +"""Print 'Hello World' every two seconds, using a callback.""" + +import asyncio + + +def print_and_repeat(loop): + print('Hello World') + loop.call_later(2, print_and_repeat, loop) + + +if __name__ == '__main__': + loop = asyncio.get_event_loop() + print_and_repeat(loop) + loop.run_forever() diff --git a/examples/hello_coroutine.py b/examples/hello_coroutine.py new file mode 100644 index 0000000..8ad682d --- /dev/null +++ b/examples/hello_coroutine.py @@ -0,0 +1,15 @@ +"""Print 'Hello World' every two seconds, using a coroutine.""" + +import asyncio + + +@asyncio.coroutine +def greet_every_two_seconds(): + while True: + print('Hello World') + yield from asyncio.sleep(2) + + +if __name__ == '__main__': + loop = asyncio.get_event_loop() + loop.run_until_complete(greet_every_two_seconds()) |