aiotools is a collection of idiomatic utilities designed to reduce boilerplate code when working with `asyncio`. It provides robust solutions for safe cancellation, structured concurrency through `TaskScope`, asynchronous context managers, multi-process server daemons, and other high-level coroutine utilities. The library is actively maintained and currently at version 2.2.3, with a release cadence that includes regular bug fixes and feature enhancements, targeting Python 3.11 and newer.
pip install aiotoolsVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates `TaskScope`, a core feature for structured concurrency. It launches multiple worker coroutines within a `TaskScope`. The `async with TaskScope()` block ensures that all child tasks created within it are either completed or cancelled before the block is exited, providing safe lifecycle management. This example also shows how to await individual tasks and retrieve their results.
Migrate usage from `aiotools.TaskGroup` to `aiotools.TaskScope` for new and existing code. `TaskScope` handles sibling task failures gracefully without cancelling others.
Replace `aiotools.func.apartial` with `functools.partial`.
Avoid using `VirtualClock` in cross-platform test suites or provide platform-specific test runners. Consider alternative time-mocking libraries for Windows compatibility if `VirtualClock`'s specific features are not strictly required.
Design your concurrency patterns carefully. Use `TaskScope` when you need independent tasks within a group that can fail without affecting siblings (e.g., background workers in a server). Use `asyncio.TaskGroup` (or `TaskScope` if its behavior matches) when tasks are interdependent and a single failure should halt the entire group.
Always use `await aiotools.cancel.cancel_and_wait(task)` when you need to cancel an `asyncio` task, instead of `task.cancel(); await task` directly. This ensures predictable handling of `CancelledError`.