InterpreterPoolExecutor workers do not inherit modifications made to sys.path before starting.
Nessuno ha ancora preso questa issue.
- Lingua principale
- Python
- Stelle
- 77.2k
- Fork
- 35.9k
- Metriche di merge delle PR
- Metriche PR in attesa
Descrizione
Bug report
Bug description:
All existing executors will reflect modifications to sys.path in the child thread/process/whatever that gets started. However the new InterpreterPoolExecutor is not copying this behavior, leading to unexpected import errors when for example running sub-intepreters inside of pytest, as the test directory is added to the sys.path in the parent interpreter but will be lost in the sub-interpreter.
This is easily worked around by using an initializer:
pool = InterpreterPoolExecutor(
max_workers=queue.concurrency,
initializer=on_initialize_worker,
initargs=(sys.path,),
)
def on_initialize_worker(parent_sys_path: list[str]):
"""
This method is called in each worker before it begins running jobs.
It can be used to perform any necessary setup, such as loading NLTK
datasets or calling ``django.setup()``.
By default, it replaces the running job's ``sys.path`` with the workers.
"""
# Unlike all other executors, the InterpreterPoolExecutor does not
# automatically inherit the parent process's sys.path. This is a
# workaround to ensure that the worker has the same sys.path as the
# parent process or tests will fail.
sys.path = parent_sys_path
This behavior makes sense for sub-interpreters themselves, but probably not for the InterpreterPoolExecutor.
cc. @ericsnowcurrently
CPython versions tested on:
3.13, 3.14
Operating systems tested on:
Linux
Guida per i contributori
Apri la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Direzione di ricerca
Inizia individuando l’implementazione CPython e i test per InterpreterPoolExecutor, quindi confronta il modo in cui gli executor esistenti gestiscono le modifiche a sys.path del processo padre prima dell’avvio dei worker. Riproduci l’errore di importazione segnalato aggiungendo un percorso prima di creare l’executor. Il lavoro è completato quando i worker ereditano quel percorso modificato senza richiedere un initializer, con una copertura di regressione per il comportamento di Python 3.13 e versioni successive.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Valutazione
- Stack tecnologico
- python
- Ambito
- backend
- Tipo di issue
- Bug
- Difficoltà
- 3/5
- Tempo stimato
- 1-2 giorni
- Stato di attività
- Ferma
- Chiarezza
- Abbastanza chiara
- Idoneità per principianti
- 35/100