A decorator that uses a trained neural network to run your function sequentially, with threads, or with processes - whichever is fastest.
pip install polymorph-ai==1.1.0 Copy PIP instructions
@adaptive_exec runs a function over a list of items sequentially, with threads, or with processes, and a small trained neural network picks whichever mode should be fastest for that job on that machine.
from polymorph_ai import adaptive_exec @adaptive_exec def process(item): ... if __name__ == "__main__": # required on Windows/macOS for processes results = process.map(items) # same as [process(x) for x in items], in order print(process.last_run) # [polymorph_ai] process: 5,000 items -> multiprocessing p=0.97 (model) | overhead 0.7 ms | total 1.84 s process(x) still works as a normal function call. Only .map() adds the automatic part.
pip install polymorph-ai Dependencies: numpy and cloudpickle. Tested on Linux with Python 3.10, 3.12 and 3.14, and on Windows with Python 3.14.
Works in Jupyter too: a function written in a notebook cell is sent to the worker processes by value (with cloudpickle), so it can still use multiprocessing on Windows and macOS.
Every .map(items) call goes through these steps:
@adaptive_exec(n_workers=4, verbose=True, cache=True) # verbose prints every decision def process(item): ... process.map(items, mode="threading") # force one mode for this call # POLYMORPH_MODE=sequential python app.py # force one mode for the whole program (A/B testing, debugging) from polymorph_ai import run, warm_up results, decision = run(some_function, items) # for functions you cannot decorate warm_up() # start the pools early (e.g. at server start-up) process.last_run (a Decision) records the chosen mode, why it was chosen, the model's probabilities, the 13 features, how many items the probe computed, and the overhead.
Model (13,603 jobs collected on 16 machine settings, see dataset/, GroupKFold by workload template, so every test job comes from code the model never trained on). Time lost compared with always picking the fastest mode:
Library (examples/demo.py: 8 everyday jobs that are not in the training data, 8 CPUs). Total time compared with perfect picks:
When does it pay off? The first call on a job pays about 0.2 s for the probe (python examples/demo.py --overhead).
python -m pytest tests -v # 28 tests, pass on Windows and Linux Project layout polymorph_ai/ the library: decorator.py, features.py, model.py + model.npz (the trained network) tests/ pytest suite examples/ demo.py (benchmark vs fixed modes) and its results/ dataset/ the training dataset (13,603 jobs) and the real-code results (92 jobs) Project links Homepage Issues Source Key dates PyPI data Data sourced directly from PyPI's database. Released: Oct 5, 2026 Latest release 1 maintainer PyPI data Data sourced directly from PyPI's database. worachat Credits Author: Worachat Songmuangnu
parallel multiprocessing threading decorator machine-learning performance
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.




