Skip to content

Commit c171bc2

Browse files
committed
merge heads
2 parents c8019f6 + c78335f commit c171bc2

File tree

2 files changed

+93
-24
lines changed

2 files changed

+93
-24
lines changed

Doc/library/asyncio-eventloop.rst

Lines changed: 50 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -649,31 +649,70 @@ Event loop examples
649649

650650
.. _asyncio-hello-world-callback:
651651

652-
Hello World with a callback
653-
---------------------------
652+
Hello World with call_soon()
653+
----------------------------
654654

655-
Print ``"Hello World"`` every two seconds using a callback scheduled by the
656-
:meth:`BaseEventLoop.call_soon` method::
655+
Example using the :meth:`BaseEventLoop.call_soon` method to schedule a
656+
callback. The callback displays ``"Hello World"`` and then stops the event
657+
loop::
657658

658659
import asyncio
659660

660-
def print_and_repeat(loop):
661+
def hello_world(loop):
661662
print('Hello World')
662-
loop.call_later(2, print_and_repeat, loop)
663+
loop.stop()
663664

664665
loop = asyncio.get_event_loop()
665-
loop.call_soon(print_and_repeat, loop)
666-
try:
667-
loop.run_forever()
668-
finally:
669-
loop.close()
666+
667+
# Schedule a call to hello_world()
668+
loop.call_soon(hello_world, loop)
669+
670+
# Blocking call interrupted by loop.stop()
671+
loop.run_forever()
672+
loop.close()
670673

671674
.. seealso::
672675

673676
The :ref:`Hello World coroutine <asyncio-hello-world-coroutine>` example
674677
uses a :ref:`coroutine <coroutine>`.
675678

676679

680+
.. _asyncio-date-callback:
681+
682+
Display the current date with call_later()
683+
------------------------------------------
684+
685+
Example of callback displaying the current date every second. The callback uses
686+
the :meth:`BaseEventLoop.call_later` method to reschedule itself during 5
687+
seconds, and then stops the event loop::
688+
689+
import asyncio
690+
import datetime
691+
692+
def display_date(end_time, loop):
693+
print(datetime.datetime.now())
694+
if (loop.time() + 1.0) < end_time:
695+
loop.call_later(1, display_date, end_time, loop)
696+
else:
697+
loop.stop()
698+
699+
loop = asyncio.get_event_loop()
700+
701+
# Schedule the first call to display_date()
702+
end_time = loop.time() + 5.0
703+
loop.call_soon(display_date, end_time, loop)
704+
705+
# Blocking call interrupted by loop.stop()
706+
loop.run_forever()
707+
loop.close()
708+
709+
.. seealso::
710+
711+
The :ref:`coroutine displaying the current date
712+
<asyncio-date-coroutine>` example uses a :ref:`coroutine
713+
<coroutine>`.
714+
715+
677716
.. _asyncio-watch-read-event:
678717

679718
Watch a file descriptor for read events

Doc/library/asyncio-task.rst

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -77,30 +77,60 @@ Coroutines (and tasks) can only run when the event loop is running.
7777

7878
.. _asyncio-hello-world-coroutine:
7979

80-
Example: "Hello World" coroutine
81-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
80+
Example: Hello World coroutine
81+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
8282

83-
Print ``"Hello World"`` every two seconds using a coroutine::
83+
Example of coroutine displaying ``"Hello World"``::
8484

8585
import asyncio
8686

8787
@asyncio.coroutine
88-
def greet_every_two_seconds():
88+
def hello_world():
89+
print("Hello World!")
90+
91+
loop = asyncio.get_event_loop()
92+
# Blocking call which returns when the hello_world() coroutine is done
93+
loop.run_until_complete(hello_world())
94+
loop.close()
95+
96+
.. seealso::
97+
98+
The :ref:`Hello World with call_soon() <asyncio-hello-world-callback>`
99+
example uses the :meth:`BaseEventLoop.call_soon` method to schedule a
100+
callback.
101+
102+
103+
.. _asyncio-date-coroutine:
104+
105+
Example: Coroutine displaying the current date
106+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
107+
108+
Example of coroutine displaying the current date every second during 5 seconds
109+
using the :meth:`sleep` function::
110+
111+
import asyncio
112+
import datetime
113+
114+
@asyncio.coroutine
115+
def display_date(loop):
116+
end_time = loop.time() + 5.0
89117
while True:
90-
print('Hello World')
91-
yield from asyncio.sleep(2)
118+
print(datetime.datetime.now())
119+
if (loop.time() + 1.0) >= end_time:
120+
break
121+
yield from asyncio.sleep(1)
92122

93123
loop = asyncio.get_event_loop()
94-
try:
95-
loop.run_until_complete(greet_every_two_seconds())
96-
finally:
97-
loop.close()
124+
# Blocking call which returns when the display_date() coroutine is done
125+
loop.run_until_complete(display_date(loop))
126+
loop.close()
98127

99128
.. seealso::
100129

101-
The :ref:`Hello World with a callback <asyncio-hello-world-callback>`
102-
example uses a callback scheduled by the :meth:`BaseEventLoop.call_soon`
103-
method.
130+
The :ref:`display the current date with call_later()
131+
<asyncio-date-callback>` example uses a callback with the
132+
:meth:`BaseEventLoop.call_later` method.
133+
104134

105135
Example: Chain coroutines
106136
^^^^^^^^^^^^^^^^^^^^^^^^^

0 commit comments

Comments
 (0)