diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -131,27 +131,16 @@ using the :meth:`sleep` function:: break await asyncio.sleep(1) loop = asyncio.get_event_loop() # Blocking call which returns when the display_date() coroutine is done loop.run_until_complete(display_date(loop)) loop.close() -The same coroutine implemented using a generator:: - - @asyncio.coroutine - def display_date(loop): - end_time = loop.time() + 5.0 - while True: - print(datetime.datetime.now()) - if (loop.time() + 1.0) >= end_time: - break - yield from asyncio.sleep(1) - .. seealso:: The :ref:`display the current date with call_later() ` example uses a callback with the :meth:`AbstractEventLoop.call_later` method. Example: Chain coroutines @@ -304,19 +293,18 @@ Future Example: Future with run_until_complete() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Example combining a :class:`Future` and a :ref:`coroutine function `:: import asyncio - @asyncio.coroutine - def slow_operation(future): - yield from asyncio.sleep(1) + async def slow_operation(future): + await asyncio.sleep(1) future.set_result('Future is done!') loop = asyncio.get_event_loop() future = asyncio.Future() asyncio.ensure_future(slow_operation(future)) loop.run_until_complete(future) print(future.result()) loop.close() @@ -336,19 +324,18 @@ Example: Future with run_forever() ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ The previous example can be written differently using the :meth:`Future.add_done_callback` method to describe explicitly the control flow:: import asyncio - @asyncio.coroutine - def slow_operation(future): - yield from asyncio.sleep(1) + async def slow_operation(future): + await asyncio.sleep(1) future.set_result('Future is done!') def got_result(future): print(future.result()) loop.stop() loop = asyncio.get_event_loop() future = asyncio.Future()