]> granicus.if.org Git - python/commitdiff
Issue #19833: add 2 examples to asyncio doc (hello world)
authorVictor Stinner <victor.stinner@gmail.com>
Mon, 2 Dec 2013 11:21:30 +0000 (12:21 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Mon, 2 Dec 2013 11:21:30 +0000 (12:21 +0100)
Doc/library/asyncio.rst

index aeb70dfecdbf23ac9ce1144371295f1061316086..b2c72cb6b95c046a492ffeaa6421194cc0abbcb5 100644 (file)
@@ -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):