|
| 1 | +******** |
| 2 | +Glossary |
| 3 | +******** |
| 4 | + |
| 5 | +.. _cancel_scope: |
| 6 | + |
| 7 | +cancel scope |
| 8 | +------------ |
| 9 | +A cancel scope is a context manager which can request the library cancels |
| 10 | +whatever task is executing in the body of the ``with`` (or ``async with``) |
| 11 | +block. A cancel scope is the key component of a :ref:`timeout context <timeout_context>`, |
| 12 | +and used in :ref:`TaskGroups / Nurseries <taskgroup_nursery>` to cancel any remaining child tasks if one raises an |
| 13 | +exception. |
| 14 | + |
| 15 | +* Trio has an explicit :class:`trio.CancelScope` type, and `general documentation |
| 16 | + <https://trio.readthedocs.io/en/stable/reference-core.html#cancellation-and-timeouts>`__ |
| 17 | + about cancellation and timeouts. |
| 18 | + |
| 19 | +* AnyIO similarly has :class:`anyio.CancelScope` and `documentation |
| 20 | + <https://anyio.readthedocs.io/en/stable/cancellation.html>`__ of cancellation handling. |
| 21 | + |
| 22 | +* asyncio does not have an explicit cancel-scope type, but incorporates similar semantics |
| 23 | + in :func:`asyncio.timeout` and :class:`asyncio.TaskGroup` and has `some documentation |
| 24 | + <https://docs.python.org/3/library/asyncio-task.html#task-cancellation>`__. |
| 25 | + |
| 26 | + |
| 27 | +.. _timeout_context: |
| 28 | + |
| 29 | +timeout context |
| 30 | +--------------- |
| 31 | +A context manager that enforces a timeout on a block of code, by cancelling it |
| 32 | +after a specified duration or at a preset time. The timeout can also be |
| 33 | +rescheduled after creation. They are internally implemented with a :ref:`cancel scope <cancel_scope>`, |
| 34 | +which in anyio & trio can be directly initialized with a deadline. |
| 35 | + |
| 36 | +* Trio has :func:`trio.move_on_after`, :func:`trio.move_on_at`, |
| 37 | + :func:`trio.fail_after`, :func:`trio.fail_at`, and :class:`trio.CancelScope` |
| 38 | + (`docs <https://trio.readthedocs.io/en/stable/reference-core.html#cancellation-and-timeouts>`__) |
| 39 | + |
| 40 | +* AnyIO has :func:`anyio.move_on_after`, :func:`anyio.fail_after`, and :class:`anyio.CancelScope` |
| 41 | + (`docs <https://anyio.readthedocs.io/en/stable/cancellation.html>`__) |
| 42 | + |
| 43 | +* asyncio has :func:`asyncio.timeout` and :func:`asyncio.timeout_at` |
| 44 | + (`docs <https://docs.python.org/3/library/asyncio-task.html#timeouts>`__) |
| 45 | + |
| 46 | + |
| 47 | +.. _taskgroup_nursery: |
| 48 | + |
| 49 | +TaskGroup / Nursery |
| 50 | +------------------- |
| 51 | + |
| 52 | +A collection of child Tasks that can run concurrently. Internally contains a |
| 53 | +:ref:`cancel scope <cancel_scope>` for canceling any remaining child tasks if |
| 54 | +one raises an exception. |
| 55 | + |
| 56 | +* Trio has :class:`trio.Nursery`, created with :func:`trio.open_nursery` |
| 57 | + (`docs <https://trio.readthedocs.io/en/stable/reference-core.html#tasks-let-you-do-multiple-things-at-once>`__) |
| 58 | + |
| 59 | +* AnyIO has :class:`anyio.abc.TaskGroup`, created with :func:`anyio.create_task_group` |
| 60 | + (`docs <https://anyio.readthedocs.io/en/stable/tasks.html>`__) |
| 61 | + |
| 62 | +* asyncio has :class:`asyncio.TaskGroup` since python 3.11 |
| 63 | + (`docs <https://docs.python.org/3/library/asyncio-task.html#asyncio.TaskGroup>`__) |
| 64 | + |
| 65 | + |
| 66 | +.. _cancellation: |
| 67 | +.. _cancelled: |
| 68 | + |
| 69 | +Cancelled / CancelledError |
| 70 | +-------------------------- |
| 71 | + |
| 72 | +Handling cancellation is very sensitive, and you generally never want to catch a |
| 73 | +cancellation exception without letting it propagate to the library. |
| 74 | + |
| 75 | +General documentation on cancellation in the different async libraries: |
| 76 | + |
| 77 | +* `Trio <https://trio.readthedocs.io/en/stable/reference-core.html#cancellation-and-timeouts>`__ |
| 78 | +* `AnyIO <https://anyio.readthedocs.io/en/stable/cancellation.html>`__ |
| 79 | +* `asyncio <https://docs.python.org/3/library/asyncio-task.html#task-cancellation>`__ |
| 80 | + |
| 81 | +Exception classes: |
| 82 | + |
| 83 | +* :class:`trio.Cancelled` |
| 84 | +* :func:`anyio.get_cancelled_exc_class` |
| 85 | +* :class:`asyncio.CancelledError` |
| 86 | + |
| 87 | +.. _checkpoint: |
| 88 | + |
| 89 | +Checkpoint |
| 90 | +---------- |
| 91 | +Checkpoints are points where the async backend checks for cancellation and |
| 92 | +can switch which task is running, in an ``await``, ``async for``, or ``async with`` |
| 93 | +expression. Regular checkpoints can be important for both performance and correctness. |
| 94 | + |
| 95 | +Trio has extensive and detailed documentation on the concept of |
| 96 | +:external+trio:ref:`checkpoints <checkpoints>`, and guarantees that all async |
| 97 | +functions defined by Trio will either checkpoint or raise an exception when |
| 98 | +``await``-ed. ``async for`` on Trio iterables will checkpoint before each |
| 99 | +iteration, and when exhausting the iterator, and ``async with`` will checkpoint |
| 100 | +on at least one of enter/exit. |
| 101 | + |
| 102 | +asyncio does not place any guarantees on if or when asyncio functions will |
| 103 | +checkpoint. This means that enabling and adhering to :ref:`ASYNC91x <ASYNC910>` |
| 104 | +will still not guarantee checkpoints. |
| 105 | + |
| 106 | +For anyio it will depend on the current backend. |
| 107 | + |
| 108 | +When using Trio (or an AnyIO library that people might use on Trio), it can be |
| 109 | +very helpful to ensure that your own code adheres to the same guarantees as |
| 110 | +Trio. For this we supply the :ref:`ASYNC91x <ASYNC910>` rules. To make it |
| 111 | +possible to reason the rules will also assume that all other async functions |
| 112 | +also adhere to those rules. This means you must be careful if you're using |
| 113 | +3rd-party async libraries. |
| 114 | + |
| 115 | +To insert a checkpoint with no other side effects, you can use |
| 116 | +:func:`trio.lowlevel.checkpoint`/:func:`anyio.lowlevel.checkpoint`/:func:`asyncio.sleep(0) |
| 117 | +<asyncio.sleep>` |
| 118 | + |
| 119 | +.. _channel_stream_queue: |
| 120 | + |
| 121 | +Channel / Stream / Queue |
| 122 | +------------------------ |
| 123 | +Interfaces used for communicating between tasks, processes, the network, etc. |
| 124 | + |
| 125 | +.. anyio streams is a :doc: and not a :label:, so we can't link with intersphinx :( |
| 126 | +
|
| 127 | +.. _anyio_streams: https://anyio.readthedocs.io/en/stable/streams.html#streams |
| 128 | + |
| 129 | +* Trio has :ref:`channels <channels>` for python objects and :ref:`streams <abstract-stream-api>` for bytes. |
| 130 | +* AnyIO has ``byte`` and ``object`` `streams <anyio_streams>`_ |
| 131 | +* asyncio has :ref:`queues <asyncio-queues>` for python objects and :ref:`streams <asyncio-streams>` for bytes. |
0 commit comments