Repository navigation
Add subinterpreter worker pool (Python 3.14+) - #11103
Open
RemiDesgrange wants to merge 4 commits into
Open
RemiDesgrange wants to merge 4 commits into
RemiDesgrange wants to merge 4 commits into
Conversation
…in interpreter Rework the subinterpreter pool so only the trace function and message data cross into a subinterpreter, while the accept/result callbacks run in the main interpreter that owns the broker connection. - Create one subinterpreter per pool slot at start and set each up like a spawned prefork child (task registration, logging, tracers, worker_process_init), copying the main interpreter's sys.path first. - Report failures to run a task at all as WorkerLostError. - Warn that time limits are unsupported; terminate_job raises. - Fix the pool alias typo and only register it when concurrent.interpreters is available (Python 3.14+). Co-Authored-By: Claude Opus 5.5 <[email protected]>
- terminate_job() now warns instead of raising: it is called on cold shutdown and for revoke(terminate=True), where raising aborted the shutdown handler. The task runs to completion; a job still waiting for an interpreter is cancelled through ApplyResult.terminate(). - Log a hint when a subinterpreter cannot be set up, typically because a task module imports an extension module that does not support subinterpreters (pydantic-core, gevent, psutil, ...). - Smoke tests run a worker with --pool=subinterpreter on the dev image built for Python 3.14 (new PYTHON_VERSION build arg), with a task module that only depends on Celery. - Document the pool, its limitations, and add it to the API reference. Co-Authored-By: Claude Opus 5.5 <[email protected]>
- A running subinterpreter cannot be interrupted, so on revoke with terminate=True, cold shutdown or soft time limit the pool now asks the task to stop through a per-interpreter queue, which task code checks with celery.concurrency.subinterpreter.stop_requested(). - Keep ApplyResult objects alive until their job is done: requests only hold a weak reference, so terminate() never reached the job. - Support worker_max_tasks_per_child: replace a subinterpreter after N tasks, sending worker_process_shutdown in it before closing it. - On startup failure, wait for every subinterpreter set up and close those created, instead of leaking them. Co-Authored-By: Claude Opus 5.5 <[email protected]>
auvipy
self-requested a review
November 14, 2026 02:17
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Note: Before submitting this pull request, please review our contributing
guidelines.
Description
Adds a
subinterpreterworker pool (celery worker -P subinterpreter, Python 3.14+). It runs tasks in parallel in isolated interpreters of the worker process (PEP 734,concurrent.interpreters), each with its own GIL. CPU-bound tasks get parallelism likeprefork, with the lower memory and startup cost of threads.Design
ThreadPoolExecutorin the main interpreter drives a fixed set of subinterpreters, one per concurrency slot.fast_trace_task/trace_task_ret) and the message data cross into a subinterpreter. The accept, success and failure callbacks (acks, events, result handling) run in the main interpreter, which owns the broker connection.spawn: it registers tasks on the unpickled app, imports the task modules, sets up logging, rebuilds the tracers and sendsworker_process_init. It also copies the main interpreter'ssys.path, which subinterpreters don't inherit (e.g. the current directory added bycelery -A).WorkerLostError.Stopping running tasks
A running subinterpreter can't be interrupted. So on
revoke(terminate=True), cold shutdown or a soft time limit, the pool asks the task to stop through a per-interpreter queue. Task code checks it withcelery.concurrency.subinterpreter.stop_requested():A task that doesn't check runs to completion, and the worker waits for it, even on cold shutdown. Returning normally after a stop request records a success; raising
Ignorekeeps the state the worker set. Hard time limits aren't supported and are ignored with a warning at startup.Recycling
--max-tasks-per-childreplaces a subinterpreter after N tasks;worker_process_shutdownis sent in it before it closes.--max-memory-per-childisn't supported, because memory is only measured for the whole process.Limitations
pydantic-core,geventandpsutil. A task module importing one stops the worker at startup. This is documented.Tests
t/unit/concurrency/test_subinterpreter.py, skipped below 3.14.t/smoke/tests/test_subinterpreter_pool.pyruns a worker with--pool=subinterpreterin Docker, with Redis and RabbitMQ brokers. It covers pool stats, tasks running in subinterpreters, a canvas, failures, warm and cold shutdown, stop requests on revoke, cold shutdown and soft time limit, and--max-tasks-per-child.PYTHON_VERSIONbuild arg (default unchanged, 3.13); this test builds it on 3.14.t/smoke/subinterpreter_tasks.py, becauset.integration.tasksimports pydantic and pytest-celery's tasks import psutil.Docs
docs/userguide/concurrency/subinterpreter.rst, an entry in the concurrency overview, an API reference page and a changelog entry.concurrent.interpretersis added toautodoc_mock_imports, since docs are built on Python < 3.14.Noticed, not changed here
threadspool has the same cold-shutdown problem fixed here:BasePool.terminate_jobraisesNotImplementedErrorfrom the cold-shutdown handler, andRequestonly keeps a weak reference to itsApplyResult, which also lacksterminate().chain(...).set(queue=...)doesn't route the tasks published later in the chain to that queue, with any pool.🤖 Generated with Claude Code