A Python library for executing iterables in parallel with flexible executors
Project description
iter-executor
A Python library for executing iterables in parallel with flexible executors.
Installation
pip install iter-executor
Or install from source:
git clone https://github.com/lh9171338/iter-executor.git
cd iter-executor
pip install -e .
Quick Start
import time
from iter_executor import ProcessExecutor
# Define a task function
def process(a, b, delay=0.1):
time.sleep(delay)
return a + b
# Prepare data
a_list = [1, 2, 3, 4, 5]
b_list = [10, 20, 30, 40, 50]
# Execute in parallel using multiple processes
executor = ProcessExecutor(process, nprocs=4)
results = executor.run(a_list, b_list, delay=0.1)
print(results) # [11, 22, 33, 44, 55]
Executors
Sequential Execution
| Executor | Description |
|---|---|
SequentialExecutor |
Execute tasks sequentially in a single process |
Process-based Executors
| Executor | Description |
|---|---|
ProcessExecutor |
Execute tasks in parallel using multiple processes |
AutoProcessExecutor |
Process executor that automatically falls back to sequential execution when nprocs=1 |
BoundedProcessExecutor |
Process executor with bounded resource slots (e.g., GPU ports) |
AutoBoundedProcessExecutor |
Auto-switching bounded process executor |
Thread-based Executors
| Executor | Description |
|---|---|
ThreadExecutor |
Execute tasks in parallel using a thread pool |
AutoThreadExecutor |
Automatically fallback to sequential execution when nworkers=1 |
Async Executors
| Executor | Description |
|---|---|
AsyncExecutor |
Execute async tasks concurrently in a single process |
AsyncProcessExecutor |
Execute async tasks across multiple processes |
AutoAsyncProcessExecutor |
Auto-switching async process executor |
Parallel Executors (Data Splitting)
| Executor | Description |
|---|---|
TaskParallelExecutor |
Split data and process each item with a single-task function |
BatchParallelExecutor |
Split data and process batches with a batch function |
AutoTaskParallelExecutor |
Auto-switching task parallel executor |
AutoBatchParallelExecutor |
Auto-switching batch parallel executor |
Usage Examples
Using ProcessExecutor
from iter_executor import ProcessExecutor
def compute(x, y, op="+"):
if op == "+":
return x + y
return x - y
executor = ProcessExecutor(compute, nprocs=4)
results = executor.run([1, 2, 3], [4, 5, 6], op="+")
# results: [5, 7, 9]
Using ThreadExecutor
from iter_executor import ThreadExecutor
def io_task(url, timeout=10):
# Simulate I/O operation
return f"Downloaded: {url}"
executor = ThreadExecutor(io_task, nworkers=8)
results = executor.run(["url1", "url2", "url3"], timeout=5)
Using AsyncExecutor
import asyncio
from iter_executor import AsyncExecutor
async def async_task(item, delay=0.1):
await asyncio.sleep(delay)
return item * 2
executor = AsyncExecutor(async_task)
results = executor.run([1, 2, 3, 4, 5], delay=0.1)
# results: [2, 4, 6, 8, 10]
Using BatchParallelExecutor
from iter_executor import BatchParallelExecutor
def process_batch(items_a, items_b, factor=1):
return [a + b * factor for a, b in zip(items_a, items_b)]
executor = BatchParallelExecutor(process_batch, nprocs=4)
results = executor.run([1, 2, 3, 4], [10, 20, 30, 40], factor=2)
# results: [21, 42, 63, 84]
Executor Selection Guide
| Scenario | Recommended Executor |
|---|---|
| CPU-bound tasks | ProcessExecutor |
| I/O-bound tasks | ThreadExecutor or AsyncExecutor |
| Need GPU/port binding | BoundedProcessExecutor |
| Async functions + multiprocessing | AsyncProcessExecutor |
| Batch data processing | BatchParallelExecutor |
| Single-task function + data splitting | TaskParallelExecutor |
| Uncertain about process count | Use Auto* series executors |
API Reference
License
This project is licensed under the MIT License - see the LICENSE file for details.
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
iter_executor-1.0.0.tar.gz
(11.8 kB
view details)
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 iter_executor-1.0.0.tar.gz.
File metadata
- Download URL: iter_executor-1.0.0.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4c70109a6b306558eb1f1a75991d9d1add1b4a63c46621848ccf508bf4065e70
|
|
| MD5 |
1e75f9d9fa697565babb76dc3e851055
|
|
| BLAKE2b-256 |
0fd4bec392041a5429712d09d1dc0bfb953e3ea604ade7cdd63d9b5b0ba53ad5
|
File details
Details for the file iter_executor-1.0.0-py3-none-any.whl.
File metadata
- Download URL: iter_executor-1.0.0-py3-none-any.whl
- Upload date:
- Size: 9.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
46d019b7d2e5cec3e21438a35cafbf9e6e986c3937252981a8d19df141943454
|
|
| MD5 |
fe9be963d3eb33dedad5b86325610f83
|
|
| BLAKE2b-256 |
203500d65f429be1fd464adfe4329c913913aa236547c70fa89157593b4cdd62
|