[Python-checkins] cpython: Issue #19833: add 2 examples to asyncio doc (hello world)
victor.stinner
python-checkins at python.org
Mon Dec 2 12:42:14 CET 2013
http://hg.python.org/cpython/rev/b25458022965
changeset: 87695:b25458022965
user: Victor Stinner <victor.stinner at gmail.com>
date: Mon Dec 02 12:21:30 2013 +0100
summary:
Issue #19833: add 2 examples to asyncio doc (hello world)
files:
Doc/library/asyncio.rst | 36 +++++++++++++++++++++++++++++
1 files changed, 36 insertions(+), 0 deletions(-)
diff --git a/Doc/library/asyncio.rst b/Doc/library/asyncio.rst
--- a/Doc/library/asyncio.rst
+++ b/Doc/library/asyncio.rst
@@ -550,6 +550,42 @@
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):
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list