[Async-sig] Blog post: Timeouts and cancellation for humans
Nathaniel Smith
njs at pobox.com
Sat Jan 13 05:32:49 EST 2018
On Thu, Jan 11, 2018 at 7:49 PM, Dima Tisnek <dimaqq at gmail.com> wrote:
> Very nice read, Nathaniel.
>
> The post left me wondering how cancel tokens interact or should
> logically interact with async composition, for example:
>
> with move_on_after(10):
> await someio.gather(a(), b(), c())
>
> or
>
> with move_on_after(10):
> await someio.first/race(a(), b(), c())
>
> or
>
> dataset = someio.Future(large_download(), move_on_after=9999)
>
> task a:
> with move_on_after(10):
> use((await dataset)["a"])
>
> task b:
> with move_on_after(10):
> use((await dataset)["b"])
It's funny you say "async composition"... Trio's concurrency primitive
(nurseries) is closely related to the core concurrency primitive in
Communicating Sequential Processes, which they call "parallel
composition". (Basically, if P and Q are processes, then "P || Q" is
the process that runs both P and Q in parallel and then finishes when
they've both finished.) If you were using that as your primitive, then
tasks would form an orderly tree and this wouldn't be a problem :-).
Given asyncio's actual primitives though, then yeah, this is clearly
the big question, and I doubt there are any simple answers; so far my
ambition has just been to articulate the problem well enough to start
that conversation (see also the "asyncio" section in the blog post).
One possibility might be a hybrid cancel token / cancel scope API:
create a first class cancel token API like C# has, enhance make the
low-level asyncio APIs to use them, and then on top of that add
mechanisms to attach a stack of implicitly-applied cancel tokens to
each task? That's just a vague handwave of an idea so far though.
Note that last case is the one where asyncio cancellation semantics
are already... well, surprising, anyway. If you cancel task a then
task b will receive a CancelledError, even though task a was not
cancelled. (I talked about this a bit in my "Some thoughts ..." blog
post; search for "spooky-cancellation-at-a-distance.py".)
-n
--
Nathaniel J. Smith -- https://vorpus.org
More information about the Async-sig
mailing list