TaskGroup and Nursery are powerful tools for structured concurrency that automatically manage the lifecycle of multiple concurrent tasks. However,
when they are used to spawn only a single task that doesn’t need the nursery/TaskGroup instance itself, they add unnecessary overhead and
complexity.
The main issues with using TaskGroup and Nursery for single tasks are:
- Unnecessary overhead: Creating a nursery or TaskGroup involves additional setup and teardown that serves no purpose when only
one task is spawned
- Misleading code: The presence of a nursery suggests that multiple tasks will be managed, which can be confusing
- Reduced readability: The structured concurrency syntax is more verbose than a simple function call
What is the potential impact?
- Performance: Minor performance overhead from unnecessary nursery/TaskGroup creation
- Maintainability: Code is more complex than necessary, making it harder to understand and maintain
- Code clarity: The intent of the code is obscured by unnecessary structured concurrency constructs