diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2013-12-09 11:40:17 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2013-12-09 11:40:17 (GMT) |
commit | 45c2fd9f8a6f9d035f86f7c4a5e30d7020203138 (patch) | |
tree | a4d82bc0eb8b3d647a0d929f51c885111d02f80b /Doc | |
parent | 52ce3b04d094b568c303968b2273e2322b422e38 (diff) | |
download | cpython-45c2fd9f8a6f9d035f86f7c4a5e30d7020203138.zip cpython-45c2fd9f8a6f9d035f86f7c4a5e30d7020203138.tar.gz cpython-45c2fd9f8a6f9d035f86f7c4a5e30d7020203138.tar.bz2 |
asyncio doc: add an example with Future
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/asyncio-task.rst | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/Doc/library/asyncio-task.rst b/Doc/library/asyncio-task.rst index 920562f..360acc9 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 <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). + |