Skip to content

Implement max_tasks_per_child for process and reusable executors - #685

Open
Aschenbath wants to merge 2 commits into
joblib:masterfrom
Aschenbath:feat/max-tasks-per-child-373
Open

Aschenbath wants to merge 2 commits into
joblib:masterfrom
Aschenbath:feat/max-tasks-per-child-373

Conversation

@Aschenbath

@Aschenbath Aschenbath commented Nov 7, 2026 •

Copy link
Copy Markdown

Workers in long-running pools can accumulate memory or native state across many calls. Add an opt-in max_tasks_per_child limit to ProcessPoolExecutor and get_reusable_executor, replacing workers after that many completed calls while preserving pending futures. The default None keeps the existing unlimited behavior.

Each submitted call counts once, including calls that raise or produce an unpicklable result. Each map chunk counts once; cancellation and argument serialization failures in the parent do not consume a worker's allowance. The initializer runs in each replacement, and changing the limit recreates a reusable executor under reuse="auto". The option supports loky, spawn and forkserver, and rejects fork.

Worker startup state remains available to the manager after asynchronous shutdown or executor collection. The existing exit handshake handles retirement, nested cleanup stays interruptible by kill_workers=True, and requested resize exits are distinguished from genuine timeout and memory warnings.

Validation:

  • Linux CPython 3.12: full suite with --skip-high-memory reports 368 passed, 25 skipped and 3 failures. All three reproduce on the unchanged base: BusyBox lsof output differs from the expected format; the Docker Desktop WSL distribution rejects the Docker CLI used by the cgroup test; and this musl runtime does not reject a bounded-semaphore over-release.
  • Linux CPython 3.14: all 35 new cases plus the existing timeout tests pass (38 passed). Windows CPython 3.12: 28 passed, 1 platform skip.
  • A separate environment without psutil passes 33 new cases, with only the two optional process-tree assertions skipped. Those two pass with psutil installed.
  • An independently installed wheel passes worker recycling, all three start methods, map chunks, asynchronous shutdown, collection and reuse checks.
  • Independent lifecycle probes cover custom reducers, environment and initializer preservation after collection/shutdown, nested forced termination, repeated resize, and 30 synchronized quota-boundary shrink cycles with warnings treated as errors.
  • Both lifecycle regressions were reproduced before their fixes. Black 25.1 and git diff --check pass.
  • Enable coverage.py subprocess collection for the locked pytest-cov 7 environment. Existing worker tests were running but their child-process lines were unmeasured. The same 9 worker cases leave all 10 new worker lines unmeasured before this configuration and cover all 10 afterward; all 35 new cases also pass with coverage enabled. This is the same shared coverage setup used by Release the resource tracker working directory on Windows #673, whose resource-tracker change is separate.
  • Sphinx HTML builds with gallery execution disabled. Strict -W reports the same 13 existing diagnostics on the changed source and unchanged base; there are no new diagnostics.

Closes #373.

AI assistance: OpenAI Codex prepared this change, tests and description. Separate automated agents reviewed the implementation and ran independent probes; this is not human maintainer review.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement max_tasks_per_child

1 participant