Skip to content

Commit

Permalink
expand open_service_nursery() example
Browse files Browse the repository at this point in the history
  • Loading branch information
belm0 committed Jul 9, 2021
1 parent 57ec551 commit 7091c8a
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions tricycle/_service_nursery.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,26 @@ async def open_service_nursery() -> AsyncIterator[trio.Nursery]:
``conn`` to remain usable in any ``finally`` or ``__aexit__``
blocks in ``communicate_with_websocket()``. With a regular
nursery, this is not guaranteed; with a service nursery, it is.
Child tasks spawned using ``start()`` gain their protection from
An example hinting at general usage::
@asynccontextmanager
async def open_websocket(host, port):
async with open_service_nursery() as nursery:
try:
# ... make some child tasks ...
yield connection
finally:
# The yield body is already cancelled, and
# child tasks are still available here for cleanup...
pass
Now, anything in the body of the ``open_websocket()`` context, including
``communicate_with_websocket()``, will be given first opportunity to cancel
gracefully. Subsequently, the ``finally`` block in the ``open_websocket()``
implementation runs, and tasks spawned within the ``try`` body are still
available during cleanup.
Note that child tasks spawned using ``start()`` gain their protection from
premature cancellation only at the point of their call to
``task_status.started()``.
"""
Expand Down

0 comments on commit 7091c8a

Please sign in to comment.