Supervisor
This is a superseding replacement of PersistentTaskGroup and recommend to use in new codes.
- class Supervisor(context: Context | None = None)[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
aclose()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.TaskGroupis 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
SupervisorScopeand Javascript’sPromise.allSettled(), whileasyncio.TaskGroupprovides the same analogy to Kotlin’sCoroutineScopeand 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.Added in version 2.0.