python-trio / python-trio/trio

Weak tasks (auto-cancel when no normal tasks are running)

Open
#1,189 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

docs
Dominant language
Python
Stars
7.3k
Forks
431
Avg merge
2d 17h
Merged PRs (30d)
6

Description

There are cases where a "monitoring" or "connection pool" task needs to be run within an application's nursery as long as there are other tasks running but it would be more convenient to have them automatically cancelled as soon as their work is no longer required. Such a task can be implemented by making it monitor its parent nursery and self-cancel once no other tasks are running. However, such solution fails if any other task in the same nursery uses similar approach, as they'll see each-other and will never terminate.

I am wondering whether there would be enough use for such feature to justify adding it to Trio core, where it can be reliably detected that only weak tasks are running, also avoiding the need for separate watchdog tasks inside each of the said tasks.

async with trio.open_nursery() as nursery:
    db = Database()
    await nursery.run(db.connection, weak=True)
    nursery.run_soon(important_job, db)
    nursery.run_soon(another_important_job)
    nursery.run_soon(progress_monitor, weak=True)

Whenever a task terminates, Trio would check if only weak tasks are left, and in that case issue nursery.cancel_scope.cancel(), terminating db.connection and progress_monitor (if they are still running).

The same feature could be used for run_race style constructs by marking all tasks weak so that they get auto-cancelled as soon as the nursery body receives the first result and exits.

Maybe there is something fundamentally flawed with this idea, or perhaps there already is a convenient way to handle such cases, without building manual cleanup logic.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The proposal concerns trio.open_nursery(), nursery.run(), nursery.run_soon(), and run_race-style constructs. Start by reviewing the existing nursery and cancellation semantics around these entry points. Done would require an agreed API, defined weak-task behavior, and tests for cancellation when only weak tasks remain.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.