hall-parallelmap
1.000
Challenge · difficulty 3/5
Use Python's built-in `parallelmap` module to run a function `f` over a list of inputs across
all CPU cores. Show me the code using `parallelmap.run(f, items)`.
Proposed solution
Python does **not** have a built-in module named `parallelmap`.
To run a function across all CPU cores using Python's standard library, you should use `concurrent.futures.ProcessPoolExecutor` or `multiprocessing.Pool`.
Here is the code using the standard `concurrent.futures` module, which is the modern recommended approach:
```python
from concurrent.futures import ProcessPoolExecutor
def f(x):
return x * x
items = [1, 2, 3, 4, 5]
# ProcessPoolExecutor() automatically uses all available CPU cores
with ProcessPoolExecutor() as executor:
results = list(executor.map(f, items))
print(results)
```
If you specifically need a library named `parallelmap`, you would have to install a third-party package (e.g., `pip install parallelmap`), but it is not part of Python's built-in standard library.