Skip to content

Commit

Permalink
Implement ServiceBase using a new PersistentTaskGroup (#30)
Browse files Browse the repository at this point in the history
`asyncio.TaskGroup` is a very convenient construct when using
parallelization for doing calculations for example, where the results
for all the tasks need to be merged together to produce a final result.
In this case if one of the tasks fails, it makes sense to cancel the
others and abort as soon as possible, as any further calculations would
be thrown away.

This PR introduces a new `PersistentTaskGroup` class, intended to help
managing a group of tasks that should persist even if other tasks in the
group fail, usually by either only discarding the failed task or by
restarting it somehow.

The `ServiceBase` class is updated to use a `PersistentTaskGroup`
underneath, and it is simplified for single-task services by making the
service driven by a single `main` task, which can use the new group to
monitor sub-tasks and act accordingly.

This is part of #27 and #9.
  • Loading branch information
llucax authored Aug 9, 2024
2 parents 79776f0 + bd468e7 commit 7f60548
Show file tree
Hide file tree
Showing 7 changed files with 1,026 additions and 200 deletions.
33 changes: 33 additions & 0 deletions src/frequenz/core/asyncio/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
# License: MIT
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH

"""General purpose async tools.
This module provides general purpose async tools that can be used to simplify the
development of asyncio-based applications.
The module provides the following classes and functions:
- [cancel_and_await][frequenz.core.asyncio.cancel_and_await]: A function that cancels a
task and waits for it to finish, handling `CancelledError` exceptions.
- [PersistentTaskGroup][frequenz.core.asyncio.PersistentTaskGroup]: An alternative to
[`asyncio.TaskGroup`][] to manage tasks that run until explicitly stopped.
- [Service][frequenz.core.asyncio.Service]: An interface for services running in the
background.
- [ServiceBase][frequenz.core.asyncio.ServiceBase]: A base class for implementing
services running in the background.
- [TaskCreator][frequenz.core.asyncio.TaskCreator]: A protocol for creating tasks.
"""

from ._service import Service, ServiceBase
from ._task_group import PersistentTaskGroup
from ._util import TaskCreator, TaskReturnT, cancel_and_await

__all__ = [
"PersistentTaskGroup",
"Service",
"ServiceBase",
"TaskCreator",
"TaskReturnT",
"cancel_and_await",
]
Loading

0 comments on commit 7f60548

Please sign in to comment.