Skip to content

Commit 8cbc886

Browse files
committed
updated
1 parent a6f559f commit 8cbc886

1 file changed

Lines changed: 22 additions & 6 deletions

File tree

README.md

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@ Make multi-threaded concurrency backward- and forward-compatible for the free-th
55

66
---
77

8-
### What?
8+
### Introduction
99

1010
`ConditionalThreadPoolExecutor` is a drop-in replacement for `ThreadPoolExecutor`
11-
that automatically adapts to the runtime environment.
11+
that adapts to the runtime environment. When running under free-threaded Python with the GIL disabled (3.14t) it behaves like a normal thread pool. However, when running under a GIL-enabled build, it falls back on single-threaded execution.
1212

13-
When running under **free-threaded Python (3.13t)** or when threads
14-
are actually useful, it behaves like a normal thread pool.
13+
The reason this is necessary is that CPU-bound processes that use multi-threading with the GIL can be much slower than single-threaded execution.
14+
`ConditionalThreadPoolExecutor` permits having a single implementation that will perform optimally regardless of if the GIL is enabled or not.
1515

16-
When running under a traditional GIL build, it **falls back to single-threaded**
17-
execution to avoid detrimental thread overhead.
1816

1917
### Why?
2018

@@ -25,6 +23,24 @@ ConditionalThreadPoolExecutor detects when threads will be ineffective and avoid
2523

2624
### Example
2725

26+
```python
27+
>>> import numpy as np
28+
>>> array = np.arange(100_000_000).reshape(100_000, 1_000)
29+
>>> func = lambda row: (row[row % 2 == 0]**2).sum()
30+
>>> with ConditionalThreadPoolExecutor(max_workers=22) as ex:
31+
... %time _ = np.fromiter(ex.map(func, array), dtype=float, count=array.shape[0])
32+
...
33+
CPU times: user 1.35 s, sys: 0 ns, total: 1.35 s
34+
Wall time: 1.35 s
35+
>>> with ThreadPoolExecutor(max_workers=22) as ex:
36+
... %time _ = np.fromiter(ex.map(func, array), dtype=float, count=array.shape[0])
37+
...
38+
CPU times: user 6.69 s, sys: 2.33 s, total: 9.02 s
39+
Wall time: 6.02 s
40+
41+
```
42+
43+
2844
```python
2945
from conditional_futures import ConditionalThreadPoolExecutor
3046

0 commit comments

Comments
 (0)