A drop-in Python map replacement featuring parallel and batch processing.
Project description
Fluentmap
Fluentmap provides a drop-in Python map replacement featuring parallel and batch processing.
Features
- Use
executorto run tasks in parallel. - Use
batch_size/chunk_sizeto send parameters in batches/chunks. - Use
num_prepareto prepare data in advance for better performance. - Call
on_returnhook to process the return value of each task.
Installation
Fluentmap is available on PyPI:
pip install fluentmap
Usage
Drop-in replacement
You can start to use fluentmap just like built-in map:
from typing import Any, List
from fluentmap import map
items: List[str] = [...]
def heavy_task(item: str) -> Any:
"""Suppose this function represents a computationally expensive task."""
def postprocessing(result: Any):
"""Suppose this function represents a postprocessing task."""
for result in map(heavy_task, items):
postprocessing(result)
Parallel processing
As heavy_task is a computationally expensive task, you can use executor to
run it in parallel.
from concurrent.futures import ProcessPoolExecutor
from fluentmap import map
# ......
with ProcessPoolExecutor() as executor:
# each heavy_task invocation runs in a separate process
for result in map(heavy_task, items, executor=executor):
postprocessing(result)
Batch/chunk processing
You can use batch_size/chunk_size to send arguments in batches/chunks.
The difference between them is that batch_size packs multiple arguments into a batch
before sending them to the function, therefore the function needs to be modified to
handle a list of arguments.
On the other hand, chunk_size packs multiple arguments into a chunks before passing
them to executor workers, while workers still process each argument sequentially.
from concurrent.futures import ProcessPoolExecutor
from typing import Any, List
from fluentmap import map
# ......
def heavy_task_in_batch(item: List[str]) -> Any:
"""Note that `item` is a list since when `batch_size` is set,
fluentmap will concatenate multiple items into a batch before sending them to
the function which is to be invoked.
"""
# An example of using `batch_size`
with ProcessPoolExecutor() as executor:
for result in map(
heavy_task_in_batch,
items,
executor=executor,
batch_size=64,
):
postprocessing(result)
# An example of using `chunk_size`
with ProcessPoolExecutor() as executor:
for result in map(
heavy_task,
items,
executor=executor,
chunk_size=32,
):
postprocessing(result)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file fluentmap-0.1.0.tar.gz.
File metadata
- Download URL: fluentmap-0.1.0.tar.gz
- Upload date:
- Size: 8.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.21.0 CPython/3.12.7 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
878d4bff07b1c07af38e877255ba16cf2c209dc6ad1377cc467fb3019ad0a4cb
|
|
| MD5 |
16df67aeb433b49ee31550b177fe42d7
|
|
| BLAKE2b-256 |
77fcff86a6b41ae29337022480d64682869fb4718af3f2342b0ef0fe9e1b863e
|
File details
Details for the file fluentmap-0.1.0-py3-none-any.whl.
File metadata
- Download URL: fluentmap-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: pdm/2.21.0 CPython/3.12.7 Linux/6.5.0-1025-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7e17e96ef7594870705934f65d37f3115cbaa0c92984685a4091dad1a9241668
|
|
| MD5 |
de6db29bd33e99cdab7e4fe4d3bc0f42
|
|
| BLAKE2b-256 |
e605284afa89da63b12c7fa0f7de63a12833c4d9ed26602101c240ba82410a50
|