Thread Pools with ThreadPoolExecutor
`concurrent.futures.ThreadPoolExecutor` manages a pool of worker threads. It is the premier tool for parallelizing synchronous, blocking I/O calls without rewriting to async 🧵.
from concurrent.futures import ThreadPoolExecutor
import time
def ping(host):
time.sleep(0.01)
return f"{host}: OK"
with ThreadPoolExecutor(max_workers=3) as pool:
results = list(pool.map(ping, ["db-1", "db-2", "db-3"]))
print(results) # ['db-1: OK', 'db-2: OK', 'db-3: OK'] 🧵
# 49. Thread Pools with ThreadPoolExecutor `concurrent.futures.ThreadPoolExecutor` manages a pool of worker threads. It is the premier tool for parallelizing synchronous, blocking I/O calls without rewriting to async 🧵. ```python from concurrent.futures import ThreadPoolExecutor import time def ping(host): time.sleep(0.01) return f"{host}: OK" with ThreadPoolExecutor(max_workers=3) as pool: results = list(pool.map(ping, ["db-1", "db-2", "db-3"])) print(results) # ['db-1: OK', 'db-2: OK', 'db-3: OK'] 🧵 ``` Blocking network calls and file operations release the GIL under the hood, allowing worker threads to make concurrent progress across the network! 🧵 --- ### Question Why is ThreadPoolExecutor effective for blocking network requests despite the CPython GIL? 🧠- [ ] a. Socket operations release the GIL while waiting on network packets 🧵 - [ ] b. ThreadPoolExecutor recompiles Python functions to C binaries on the fly - [ ] c. The GIL automatically turns off when the pool size is greater than 1 - [ ] d. Network drivers execute Python bytecode directly in hardware --- ### Challenge Use `ThreadPoolExecutor(max_workers=2)` to compute `list(executor.map(lambda x: x * 10, [1, 2, 3]))`. Print the resulting list. 🎯 <details> <summary>Solution</summary> ```python from concurrent.futures import ThreadPoolExecutor with ThreadPoolExecutor(max_workers=2) as executor: results = list(executor.map(lambda x: x * 10, [1, 2, 3])) print(results) ``` </details>