Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Persistent Task Group #29

Closed
achimnol opened this issue Dec 19, 2021 · 2 comments · Fixed by #30
Closed

Persistent Task Group #29

achimnol opened this issue Dec 19, 2021 · 2 comments · Fixed by #30
Labels

Comments

@achimnol
Copy link
Owner

achimnol commented Dec 19, 2021

Let's add a persistent task group in addition to the current aiotools.TaskGroup.

While existing TaskGroup targets to group and ensure termination of a set of tasks with a short period of lifetime (e.g., a single operation that is composed of many async tasks), PersistentTaskGroup targets to group and ensure termination of a continuously changing set of tasks that are created and terminate through a very long time (e.g., the entire process lifecycle), with proper cancellation and exception handling upon shutdown.

One key difference to TaskGroup is that it has its own fallback exception handler because gathering the results (and exceptions) of all tasks at once at the point of "exit" as MultiError is meaningless due to sparsity across time. Custom fallback exception handlers could be implemented using decorators (with functools.wraps() to keep the function signature consistent with the original coroutine).

To automatically detach terminated tasks from the tracking set, we could use weakref.WeakSet.

In fact, the above pattern is widely used in most Backend.AI components, and adding PersistentTaskGroup will reduce boilerplates for Backend.AI.

@asvetlov
Copy link

/cc @1st1
Yuri, you might consider this mode also when designing the asyncio TaskGroup class.

@achimnol
Copy link
Owner Author

To aggregate the results of belonging tasks, we could use a separate async-generator channel to subscribe the results/exceptions of tasks finished through time, though this feature would be completely optional as the main use case for PersistentTaskgroup would be server applications where most tasks would return just None while serving their results to some I/O channels such as client connections.

# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants