python-trio / python-trio/trio

Get task retvals from the nursery after it finishes (aka "fake-gathers considered harmful")

Open
#2,633 53 comments 9 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

I haven't seen a way to do this in the docs, and it certainly seems very logical/thematic if it were to work like this. The nursery is the starter and finisher of tasks, its sole and entire purpose is to start tasks and ensure they finish in some halfway-sensible way, with some sort of sane control flow.

Therefore it's only logical that the return values of those tasks are collected by the nursery. Return values shouldn't disappear into the void any more than errors. The nursery gathers errors, so it should gather retvals too.

I find myself wanting to gather return values from my tasks somewhat akin to the following, taken from the docs: running a process for an arbitrary amount of time which dynamically spawns an arbitrary number of tasks.

A little bit of searching finds me a recipe to kinda back my way into this functionality faking out a sort of trio.gather, but it seems to me to be a pretty basic primitive of structured concurrency, and if it's common enough to be a recipe, then it's common enough to implement once and get right instead of rewriting it (or at least repasting and in-place-modifying it) every time you call a nursery.

But then I'm quite new to asyncio and structured concurrency. All I know is that I greatly enjoyed NJS's blog post on control flow, and have equally enjoyed my first 100 lines of code. But I would love even more to either 1) have this primitive implemented in trio 2) understand why it's not a primitive in some way. It sure sounds like one: "nurseries spawn tasks, nurseries collect tasks, nurseries call functions, ergo nurseries collect those functions' retvals"

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

Start with the documentation recipe described in the issue and read the discussion about nursery return values and structured concurrency. Determine whether Trio should expose a nursery-based result-collection primitive and what its semantics should be. Done means the API decision is settled and the behavior is documented and covered by the project’s appropriate checks.

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.