joblib/threadpoolctl
Python helpers to limit the number of threads used in native libraries that handle their own internal threadpool (BLAS and OpenMP implementations)
What it solves
Scientific computing and data science programs often rely on native libraries like BLAS and OpenMP that maintain their own thread pools. When you nest parallelism (for example, run BLAS operations inside your own multi-threaded code), these underlying pools can oversubscribe the CPU, causing slowdowns. This project gives you tools to inspect those thread pools and cap how many threads they use, so you can avoid waste and keep performance predictable.
How it works
It works by inspecting the native shared libraries already loaded in your Python process (for example, from numpy or scipy) and exposing their thread-pool configuration. You can:
- Use the command line (
python -m threadpoolctl -i numpy scipy.linalg) to print a JSON snapshot of each library's thread pool. - Call
threadpool_info()from Python to see the same information programmatically. - Use the
threadpool_limitscontext manager or theThreadpoolControllerobject to temporarily set a maximum number of threads for specific library families (like BLAS or OpenMP). - Apply limits as decorators to functions, or manage limits inside Python thread pools by using a controller in each worker thread.
The project also handles the fact that different libraries have different limiting semantics (some set a global process-wide limit, some only affect the current thread), and it tries to choose the API that matches your intent.
Who it’s for
This is for Python developers working with numeric libraries that use native thread pools — for example, anyone using numpy, scipy, scikit-learn, xgboost, or similar tools in performance-sensitive or parallel workloads. It’s especially useful when you are combining Python-level parallelism with native parallelism and want to tune how many threads each layer uses.
Highlights
- Command-line tool for quick inspection of thread pools loaded by Python packages.
- Python API with both a simple function (
threadpool_info) and a reusable object (ThreadpoolController). - Context managers and decorators to limit thread counts within specific code sections or functions.
- Support for both BLAS libraries (OpenBLAS, MKL, FlexiBLAS) and OpenMP implementations (libgomp, libiomp, libomp).
- Can handle Python thread pools by letting you set limits in each worker thread while keeping overall control via a controller.
- Include an experimental API to switch the FlexiBLAS backend at runtime.
- Extensible: you can write and register a custom controller for other native libraries that expose a thread-limit API.
Related
- Project
- Project
- Project
- Project