diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2013-12-02 11:21:30 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2013-12-02 11:21:30 (GMT) |
commit | 4e70bb84e6de52369ca9f3eb5c76b34f421d9e92 (patch) | |
tree | d0cc295b0c5d0c01b218e03b5757028215e0fa7f | |
parent | 8dc434e092a918303ef09da61dbf8eabcb38ead4 (diff) | |
download | cpython-4e70bb84e6de52369ca9f3eb5c76b34f421d9e92.zip cpython-4e70bb84e6de52369ca9f3eb5c76b34f421d9e92.tar.gz cpython-4e70bb84e6de52369ca9f3eb5c76b34f421d9e92.tar.bz2 |
Issue #19833: add 2 examples to asyncio doc (hello world)
-rw-r--r-- | Doc/library/asyncio.rst | 36 |
1 files changed, 36 insertions, 0 deletions
diff --git a/Doc/library/asyncio.rst b/Doc/library/asyncio.rst index aeb70df..b2c72cb 100644 --- a/Doc/library/asyncio.rst +++ b/Doc/library/asyncio.rst @@ -550,6 +550,42 @@ Synchronization primitives Examples -------- +Hello World (callback) +^^^^^^^^^^^^^^^^^^^^^^ + +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) + + loop = asyncio.get_event_loop() + print_and_repeat(loop) + loop.run_forever() + + +Hello World (callback) +^^^^^^^^^^^^^^^^^^^^^^ + +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) + + loop = asyncio.get_event_loop() + loop.run_until_complete(greet_every_two_seconds()) + + +Echo server +^^^^^^^^^^^ + A :class:`Protocol` implementing an echo server:: class EchoServer(asyncio.Protocol): |