Supervisor
This is a superseding replacement of PersistentTaskGroup
and recommend to use in new codes.
- class Supervisor[source]
Supervisor is a primitive structure to provide a long-lived context manager scope for an indefinite set of subtasks. During its lifetime, it is free to spawn new subtasks at any time. If the supervisor itself is cancelled from outside or
shutdown()
is called, it will cancel all running tasks immediately, wait for their completion, and then exit the context manager block.The main difference to
asyncio.TaskGroup
is that it keeps running sibling subtasks even when there is an unhandled exception from one of the subtasks.To prevent memory leaks, a supervisor does not store any result or exception from its subtasks. Instead, the callers must use additional task-done callbacks to process subtask results and exceptions.
Supervisor provides the same analogy to Kotlin’s
SupervisorScope
and Javascript’sPromise.allSettled()
, whileasyncio.TaskGroup
provides the same analogy to Kotlin’sCoroutineScope
and Javascript’sPromise.all()
.The original implementation is based on DontPanicO’s pull request (https://github.com/achimnol/cpython/pull/31) and
PersistentTaskGroup
, but it is modified not to store unhandled subtask exceptions.New in version 2.0.