Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug: system threads persist in threadpool_limits context manager #132

Closed
cadedaniel opened this issue Jan 15, 2023 · 4 comments
Closed

Bug: system threads persist in threadpool_limits context manager #132

cadedaniel opened this issue Jan 15, 2023 · 4 comments

Comments

@cadedaniel
Copy link

Hi, thanks for the work on this package! I would like to use it but I am not getting the expected behavior.

When I import numpy inside a threadpool_limits context manager, I expect the number of threads to be limited by threadpool_limits.

What happens is that the thread count is not changed.

Is this user error or a bug?

#!/usr/bin/env python3

from threadpoolctl import threadpool_limits, threadpool_info
import psutil
import os

proc = psutil.Process(os.getpid())

print('pid', os.getpid())
print('threads:', len(proc.threads()))
print(threadpool_info())

with threadpool_limits(limits=2, user_api='blas'):
    import numpy
    print('numpy imported')
    print('threads:', len(proc.threads()))
    print(threadpool_info())

print('threads:', len(proc.threads()))
print(threadpool_info())
pid 60392
threads: 1
[]
numpy imported
threads: 36
[{'user_api': 'blas', 'internal_api': 'openblas', 'prefix': 'libopenblas', 'filepath': '/home/ray/anaconda3/lib/python3.10/site-packages/numpy.libs/libopenblas64_p-r0-15028c96.3.21.so', 'version': '0.3.21', 'threading_layer': 'pthreads', 'architecture': 'SkylakeX', 'num_threads': 36}]
threads: 36
[{'user_api': 'blas', 'internal_api': 'openblas', 'prefix': 'libopenblas', 'filepath': '/home/ray/anaconda3/lib/python3.10/site-packages/numpy.libs/libopenblas64_p-r0-15028c96.3.21.so', 'version': '0.3.21', 'threading_layer': 'pthreads', 'architecture': 'SkylakeX', 'num_threads': 36}]
@tomMoral
Copy link
Contributor

tomMoral commented Jan 15, 2023 via email

@cadedaniel
Copy link
Author

cadedaniel commented Jan 15, 2023

edited, threadpoolctl is reporting 1 thread in numpy in the context manager 🎉

Thanks for explaining. I still see 36 system threads reported by pustil. Is this expected?

pid 123361
threads: 1
[]
numpy imported
threads: 36
[{'user_api': 'blas', 'internal_api': 'openblas', 'prefix': 'libopenblas', 'filepath': '/home/ray/anaconda3/lib/python3.10/site-packages/numpy.libs/libopenblas64_p-r0-15028c96.3.21.so', 'version': '0.3.21', 'threading_layer': 'pthreads', 'architecture': 'SkylakeX', 'num_threads': 1}]
threads: 36
[{'user_api': 'blas', 'internal_api': 'openblas', 'prefix': 'libopenblas', 'filepath': '/home/ray/anaconda3/lib/python3.10/site-packages/numpy.libs/libopenblas64_p-r0-15028c96.3.21.so', 'version': '0.3.21', 'threading_layer': 'pthreads', 'architecture': 'SkylakeX', 'num_threads': 36}]
#!/usr/bin/env python3

from threadpoolctl import threadpool_limits, threadpool_info
import psutil
import os

proc = psutil.Process(os.getpid())

print('pid', os.getpid())
print('threads:', len(proc.threads()))

print(threadpool_info())

import numpy
print('numpy imported')

with threadpool_limits(limits=1, user_api='blas'):
    # Reload in case there is caching of proc.threads
    proc = psutil.Process(os.getpid())
    print('threads:', len(proc.threads()))
    print(threadpool_info())

print('threads:', len(proc.threads()))
print(threadpool_info())

@cadedaniel cadedaniel changed the title Bug: threadpool_limits does not correctly limit Bug: system threads persist in threadpool_limits context manager Jan 15, 2023
@tomMoral
Copy link
Contributor

I am actually not sure how the system threads are managed so it is not so surprising to still see them and as threads a low weights objects, this should not reduce performances.
But if you see some strange behavior, don't hesitate to report here so we can investigate further.

@cadedaniel
Copy link
Author

Ok thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants