Threadpoolctl

raw JSON →
3.6.0 verified Tue May 12 auth: no python install: verified quickstart: stale

Threadpoolctl is a Python library that provides helpers to limit the number of threads used in threadpool-backed native libraries, such as BLAS and OpenMP, commonly used in scientific computing and data science. The current version is 3.6.0, released on March 13, 2025. The library is actively maintained with regular updates to support new Python versions and improve functionality.

pip install threadpoolctl
error ImportError: No module named 'threadpoolctl'
cause The 'threadpoolctl' library has not been installed in the current Python environment.
fix
pip install threadpoolctl
error AttributeError: module 'threadpoolctl' has no attribute 'threadpool_limits'
cause The 'threadpool_limits' function (and 'threadpool_info') should be imported directly from the 'threadpoolctl' package, not accessed as an attribute of the top-level module.
fix
from threadpoolctl import threadpool_limits
error RuntimeError: threadpoolctl cannot find any threadpool library loaded in this process
cause 'threadpoolctl' could not detect any supported threadpool-backed native libraries (like OpenBLAS, MKL, or OpenMP) loaded in the current Python process.
fix
Ensure that libraries like NumPy or SciPy are imported and correctly linked to a threadpool-enabled BLAS/LAPACK implementation (e.g., OpenBLAS, MKL) before using 'threadpoolctl'.
breaking Dropped official support for Python 3.8 in version 3.6.0.
fix Upgrade to Python 3.9 or later to maintain compatibility.
gotcha Using threadpoolctl with both libomp (LLVM OpenMP) and libiomp (Intel OpenMP) loaded may cause crashes or deadlocks.
fix Ensure only one OpenMP library is loaded to prevent potential issues.
breaking Required package 'numpy' is not installed.
fix Install the 'numpy' package using pip: `pip install numpy`
breaking Required package 'numpy' is not found. Ensure all necessary dependencies are installed.
fix Install the missing package using 'pip install numpy' or ensure the package is listed in your project's 'requirements.txt' and installed.
conda install -c conda-forge threadpoolctl
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.04s 17.9M
3.10 slim (glibc) - - 0.03s 18M
3.11 alpine (musl) - - 0.07s 19.7M
3.11 slim (glibc) - - 0.07s 20M
3.12 alpine (musl) - - 0.06s 11.6M
3.12 slim (glibc) - - 0.06s 12M
3.13 alpine (musl) - - 0.05s 11.2M
3.13 slim (glibc) - - 0.07s 12M
3.9 alpine (musl) - - 0.04s 17.4M
3.9 slim (glibc) - - 0.04s 18M

This script demonstrates how to introspect threadpool information and limit the number of threads used by BLAS libraries using threadpoolctl.

import numpy as np
from threadpoolctl import threadpool_info, threadpool_limits

# Introspect threadpool information
info = threadpool_info()
for lib in info:
    print(f"Library: {lib['internal_api']} ({lib['user_api']})")
    print(f"  Version: {lib['version']}")
    print(f"  Threads: {lib['num_threads']}")
    print(f"  Path: {lib['filepath']}")

# Limit threads for BLAS
with threadpool_limits(limits=1, user_api='blas'):
    a = np.random.randn(1000, 1000)
    a_squared = a @ a