You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Commit 3ed8ce6
Browse filesBrowse the repository at this point in the historyBrowse files
Copy file name to clipboardExpand all lines: README.md
+30-33Lines changed: 30 additions & 33 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -176,10 +176,10 @@ in specific sections of your Python program:
176
176
... a_squared = a @ a
177
177
```
178
178
179
-
The threadpools can also be controlled via the object oriented API, which is especially
180
-
useful to avoid searching through all the loaded shared libraries each time. It will
181
-
however not act on libraries loaded after the instantiation of the
182
-
`ThreadpoolController`:
179
+
The threadpools can also be controlled via the object oriented API, which is
180
+
especially useful to avoid searching through all the loaded shared libraries
181
+
each time. **Note that it will not act on libraries loaded after the instantiation
182
+
of the `ThreadpoolController`!**
183
183
184
184
```python
185
185
>>>from threadpoolctl import ThreadpoolController
@@ -225,44 +225,40 @@ controlled libraries in that thread.** With Python's
225
225
`concurrent.futures.ThreadPoolExecutor`, you can do so by passing in an
226
226
initializer function that will get called on thread startup.
227
227
228
-
```python
229
-
from threadpoolctl import threadpool_limits
230
-
from concurrent.futures import ThreadPoolExecutor
231
-
232
-
# This top-level limiter doesn't actually change the limits initially; it is
233
-
# there to ensure the limits are reset _after_ the Python thread pool is done.
234
-
# This is necessary because some underlying limiting APIs operate on a
235
-
# process-wide basis.
236
-
with threadpool_limits():
237
-
# Make sure each Python worker thread also calls threadpool_limits(). If
238
-
# you're using another thread pool class, you will need to do so some other
239
-
# way.
240
-
with ThreadPoolExecutor(4, initializer=lambda: threadpool_limits(limits=1)) as pool:
241
-
# ... run some BLAS-using code in the thread pool ...
242
-
pool.map(somefunc, someargs)
243
-
```
244
-
245
-
Whenever `threadpool_limits` is called, it needs to do some work (inspecting and getting access to third-party shared libraries) that can take some time.
246
-
To prevent the performance cost of doing this work every time, you can reuse a
247
-
`ThreadpoolController` object:
228
+
Whenever `threadpool_limits` is called, it creates a new `ThreadpoolController`,
229
+
which needs to do some work (inspecting and getting access to third-party shared
230
+
libraries) that can take some time. To prevent the performance cost of doing
231
+
this work in all the threads, you can reuse a `ThreadpoolController` object
232
+
across the threads.
248
233
249
234
```python
250
235
from threadpoolctl import ThreadpoolController
251
236
252
-
# This won't have any side-effects:
253
-
CONTROLLER= ThreadpoolController()
237
+
# This won't have any side-effects. Because it caches its list of loaded
238
+
# libraries, you need to create a new one if you've imported or loaded any
239
+
# relevant libraries in the interim. So storing this on module level may not be
240
+
# a good idea if you e.g. only do `import numpy` later on.
241
+
controller = ThreadpoolController()
254
242
255
243
with (
256
-
CONTROLLER.limit(),
257
-
ThreadPoolExecutor(4, initializer=lambda: CONTROLLER.limit(limits=1)) as pool,
244
+
# This top-level limiter doesn't actually change the limits initially; it
245
+
# is there to ensure the limits are reset _after_ the Python thread pool is
246
+
# done. This is necessary because some underlying limiting APIs operate on
247
+
# a process-wide basis.
248
+
controller.limit(),
249
+
# Make sure each Python worker thread also calls threadpool_limits(). If
250
+
# you're using another thread pool class, you will need to do so some other
251
+
# way.
252
+
ThreadPoolExecutor(4, initializer=lambda: controller.limit(limits=1)) as pool,
258
253
):
259
254
# ... run some BLAS-using code in the thread pool ...
260
255
pool.map(somefunc, someargs)
261
256
262
257
# Later...
258
+
controller = ThreadpoolController()
263
259
with (
264
-
CONTROLLER.limit(),
265
-
ThreadPoolExecutor(4, initializer=lambda: CONTROLLER.limit(limits=2)) as pool,
260
+
controller.limit(),
261
+
ThreadPoolExecutor(4, initializer=lambda: controller.limit(limits=2)) as pool,
266
262
):
267
263
# ... run some BLAS-using code in the thread pool ...
268
264
pool.map(somefunc, someargs)
@@ -273,11 +269,12 @@ You can also operate without a context manager:
273
269
```python
274
270
from threadpoolctl import ThreadpoolController
275
271
276
-
CONTROLLER= ThreadpoolController()
272
+
controller= ThreadpoolController()
277
273
try:
278
-
limiter =CONTROLLER.limit()
274
+
limiter =controller.limit()
279
275
with ThreadPoolExecutor(
280
-
4, initializer=lambda: CONTROLLER.limit(limits=1)) as pool:
276
+
4, initializer=lambda: controller.limit(limits=1)
277
+
) as pool:
281
278
# ... run some BLAS-using code in the thread pool ...
0 commit comments