diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2015-01-29 23:56:10 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2015-01-29 23:56:10 (GMT) |
commit | 151b23562ea6105f79d8c4b5d5fe42bb61813d96 (patch) | |
tree | dfb63d9a6f9adf97f4e225376eb70a280b714e4b | |
parent | ff5d085fde309dcbcce0e9ac3433fa8d0fb364ae (diff) | |
parent | 1077dee4575ccc43c10515de50d7c100d6ce9455 (diff) | |
download | cpython-151b23562ea6105f79d8c4b5d5fe42bb61813d96.zip cpython-151b23562ea6105f79d8c4b5d5fe42bb61813d96.tar.gz cpython-151b23562ea6105f79d8c4b5d5fe42bb61813d96.tar.bz2 |
Merge 3.4 (asyncio doc)
-rw-r--r-- | Doc/library/asyncio-dev.rst | 37 |
1 files changed, 37 insertions, 0 deletions
diff --git a/Doc/library/asyncio-dev.rst b/Doc/library/asyncio-dev.rst index 7083e60..72a06f5 100644 --- a/Doc/library/asyncio-dev.rst +++ b/Doc/library/asyncio-dev.rst @@ -40,6 +40,43 @@ Examples of effects of the debug mode: <asyncio-logger>`. +Cancellation +------------ + +Cancellation of tasks is not common in classic programming. In asynchronous +programming, not only it is something common, but you have to prepare your +code to handle it. + +Futures and tasks can be cancelled explicitly with their :meth:`Future.cancel` +method. The :func:`wait_for` function cancels the waited task when the timeout +occurs. There are many other cases where a task can be cancelled indirectly. + +Don't call :meth:`~Future.set_result` or :meth:`~Future.set_exception` method +of :class:`Future` if the future is cancelled: it would fail with an exception. +For example, write:: + + if not fut.cancelled(): + fut.set_result('done') + +Don't schedule directly a call to the :meth:`~Future.set_result` or the +:meth:`~Future.set_exception` method of a future with +:meth:`BaseEventLoop.call_soon`: the future can be cancelled before its method +is called. + +If you wait for a future, you should check early if the future was cancelled to +avoid useless operations. Example:: + + @coroutine + def slow_operation(fut): + if fut.cancelled(): + return + # ... slow computation ... + yield from fut + # ... + +The :func:`shield` function can also be used to ignore cancellation. + + .. _asyncio-multithreading: Concurrency and multithreading |