Skip to content

await in a finally block + cancellation of task group. #367

Answered by graingert
jonathanslenders asked this question in Q&A
Discussion options

You must be logged in to vote

This is part of the trio level cancellation semantics eg: https://trio.readthedocs.io/en/stable/reference-core.html#cancellation-semantics

Cancellations in Trio are “level triggered”, meaning that once a block has been cancelled, all cancellable operations in that block will keep raising Cancelled. This helps avoid some pitfalls around resource clean-up. For example, imagine that we have a function that connects to a remote server and sends some messages, and then cleans up on the way out:

with anyio.move_on_after(TIMEOUT):
    conn = make_connection()
    try:
        await conn.send_hello_msg()
    finally:
        with anyio.move_on_after(CLEANUP_TIMEOUT) as cleanup_scope:
            

Replies: 1 comment 8 replies

Comment options

You must be logged in to vote
8 replies
@smurfix
Comment options

@agronholm
Comment options

@jonathanslenders
Comment options

@agronholm
Comment options

@jonathanslenders
Comment options

Answer selected by smurfix
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #366 on September 07, 2021 16:09.