Registry / workflow / bounded-pool-executor

bounded-pool-executor

JSON →
library0.0.3pypypi✓ verified 21d ago

Bounded Pool Executor is a Python library that provides `BoundedThreadPoolExecutor` and `BoundedProcessPoolExecutor` classes, extending `concurrent.futures` to manage a fixed-size queue for tasks. This prevents memory exhaustion that can occur with the standard unbounded queues in `concurrent.futures` when submitting a large number of tasks. The current version is 0.0.3, offering a solution to prevent memory leaks in high-concurrency scenarios by blocking `submit` calls when the queue is full.

pip install bounded-pool-executor
INSTALL
IMPORT
SIG · BOUNDED-POOL-EXECU
B
bounded-pool-executor
workflowpythonv0.0.3
Install
1.5s avg
Import
125ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.0.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.138s · 17.8MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.112s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

BoundedThreadPoolExecutor
from bounded_pool_executor import BoundedThreadPoolExecutor
BoundedProcessPoolExecutor
from bounded_pool_executor import BoundedProcessPoolExecutor

This example demonstrates how to use `BoundedThreadPoolExecutor` to execute tasks while limiting the number of concurrent tasks and the size of the waiting queue. The `submit` method will block if the queue, including currently executing tasks, reaches its `max_queue_size`.

import time from bounded_pool_executor import BoundedThreadPoolExecutor def my_task(n): time.sleep(0.1) # Simulate some work return f"Task {n} completed" max_workers = 2 max_queue_size = 4 # total slots = max_workers + max_queue_size_for_waiting_tasks print(f"Using BoundedThreadPoolExecutor with {max_workers} workers and queue size {max_queue_size}") futures = [] with BoundedThreadPoolExecutor(max_workers=max_workers, max_queue_size=max_queue_size) as pool: for i in range(1, 10): print(f"Submitting task {i}...") # The submit call will block if the queue is full future = pool.submit(my_task, i) futures.append(future) print("All tasks submitted. Waiting for results...") for future in futures: print(future.result())
Debug
Known issues
gotchaExceptions raised within tasks submitted to the executors are silently ignored by default. This can lead to silent failures that are difficult to debug.
fix
Manually add `add_done_callback` to futures to check for exceptions: `future.add_done_callback(lambda f: print(f'Task error: {f.exception()}') if f.exception() else None)`.
affects: 0.0.1 to 0.0.3
gotchaThe `max_queue_size` parameter in `BoundedProcessPoolExecutor` (and `BoundedThreadPoolExecutor`) represents the sum of currently executing tasks *plus* tasks waiting in the queue. If `max_queue_size` is too small (e.g., less than `max_workers`), worker processes may sit idle even when tasks are available to be submitted, leading to underutilization.
fix
Set `max_queue_size` to `max_workers + M`, where `M` is the desired number of tasks that can be waiting in the queue. A value of at least `2 * max_workers` is often recommended to keep workers busy.
affects: 0.0.1 to 0.0.3
gotchaWhen using `BoundedProcessPoolExecutor`, functions and their arguments must be picklable. Submitting unpicklable objects (e.g., lambda functions defined in the main script, or objects containing unpicklable attributes like an executor instance itself) will cause runtime errors. This is a common limitation of Python's `multiprocessing` module.
fix
Ensure that any function or object passed to `BoundedProcessPoolExecutor.submit` is top-level (not nested) and picklable. Avoid passing `self` from a class instance that also holds the `ProcessPoolExecutor`.
affects: 0.0.1 to 0.0.3
gotchaDeadlock can occur if tasks submitted to the bounded pool attempt to submit new tasks to the *same* pool or wait for results from other tasks in the same pool, especially if `max_workers` and `max_queue_size` are small. This is a general concurrency hazard with bounded pools.
fix
Design tasks to be independent. If tasks need to submit sub-tasks, consider a separate, dedicated pool for sub-tasks, or ensure sufficient pool and queue capacity to prevent circular dependencies that lead to deadlock.
affects: 0.0.1 to 0.0.3
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'bounded_pool_executor'
The Python package is installed as `bounded-pool`, but the user is attempting to import it using a different, descriptive name that does not match the actual module name, such as 'bounded_pool_executor' or 'bounded_executor'.
fix
Ensure the import statement uses the correct module name `bounded_pool`. If not already installed, first run `pip install bounded-pool`.
`from bounded_pool import BoundedThreadPoolExecutor`
AttributeError: 'BoundedThreadPoolExecutor' object has no attribute 'map'
Users accustomed to `concurrent.futures.ThreadPoolExecutor` often expect a `map` method to apply a function to an iterable. The `BoundedThreadPoolExecutor` class from the `bounded-pool` library does not provide a `map` method, instead relying on repeated `submit` calls and iterating over futures.
fix
Instead of `executor.map(func, iterable)`, use a loop with `executor.submit` and collect the `Future` objects, then retrieve results using `future.result()` or `concurrent.futures.as_completed`.
```python
from bounded_pool import BoundedThreadPoolExecutor

def my_task(item):
    return item * 2

items =
results = []
with BoundedThreadPoolExecutor(max_workers=2, max_queue_size=2) as executor:
    futures = [executor.submit(my_task, item) for item in items]
    for future in futures:
        results.append(future.result())
print(results)
```
ModuleNotFoundError: No module named 'bounded_pool'
The `bounded-pool` package has not been installed in the current Python environment, or there is a typo in the import statement.
fix
First, install the package using pip: `pip install bounded-pool`. Then, ensure the import statement is `from bounded_pool import BoundedThreadPoolExecutor` (or `BoundedProcessPoolExecutor`).
Upgrade
Version history
0.0.3latest on PyPI · released Jun 4, 2019
Audit
Dependencies

No dependency data recorded yet.

Agent activity
19 hits · last 30 days
node
16
OpenAI (training)
1
Resources
bounded-pool-executor — pip install bounded-pool-executor · libregistry