Threadpoolctl
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.
Warnings
- breaking Dropped official support for Python 3.8 in version 3.6.0.
- gotcha Using threadpoolctl with both libomp (LLVM OpenMP) and libiomp (Intel OpenMP) loaded may cause crashes or deadlocks.
Install
-
pip install threadpoolctl -
conda install -c conda-forge threadpoolctl
Imports
- threadpool_info
from threadpoolctl import threadpool_info
- threadpool_limits
from threadpoolctl import threadpool_limits
Quickstart
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