From: Victor Stinner Date: Mon, 9 Dec 2013 11:40:17 +0000 (+0100) Subject: asyncio doc: add an example with Future X-Git-Tag: v3.4.0b2~283 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=45c2fd9f8a6f9d035f86f7c4a5e30d7020203138;p=thirdparty%2FPython%2Fcpython.git asyncio doc: add an example with Future --- diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index 920562fc1a7c..360acc92fbf5 100644 --- a/Doc/library/asyncio-task.rst +++ b/Doc/library/asyncio-task.rst @@ -408,3 +408,27 @@ Details: * ``wait_task()`` stops the event loop when ``print_sum()`` is done. + +Example: Future and get result +^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + +Example combining a :class:`Future` and a :ref:`coroutine `:: + + import asyncio + + @asyncio.coroutine + def slow_operation(future): + yield from asyncio.sleep(1) + future.set_result('Future in done!') + + loop = asyncio.get_event_loop() + future = asyncio.Future() + asyncio.Task(slow_operation(future)) + loop.run_until_complete(future) + print(future.result()) + loop.close() + +The example waits for the completion of the future (which takes 1 second). The +coroutine is responsible of the computation. The event loop is notified when +the future is done (see the :meth:`Future.set_result` method). +