Skip to main content

Parallel processing utilities using Pathos mpprocessing library

Project description

twat-mp

(work in progress)

Parallel processing utilities using the Pathos multiprocessing library. This package provides convenient context managers and decorators for parallel processing, with both process-based and thread-based pools.

Features

  • Context managers for both process and thread pools:
    • ProcessPool: For CPU-intensive parallel processing
    • ThreadPool: For I/O-bound parallel processing
  • Decorators for common parallel mapping operations:
    • amap: Asynchronous parallel map with automatic result retrieval
    • imap: Lazy parallel map returning an iterator
    • pmap: Standard parallel map (eager evaluation)
  • Automatic CPU core detection for optimal pool sizing
  • Clean resource management with context managers
  • Full type hints and modern Python features
  • Flexible pool configuration with customizable worker count

Installation

pip install twat-mp

Usage

Using Process and Thread Pools

The package provides dedicated context managers for both process and thread pools:

from twat_mp import ProcessPool, ThreadPool

# For CPU-intensive operations
with ProcessPool() as pool:
    results = pool.map(lambda x: x * x, range(10))
    print(list(results))  # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

# For I/O-bound operations
with ThreadPool() as pool:
    results = pool.map(lambda x: x * 2, range(10))
    print(list(results))  # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

# Custom number of workers
with ProcessPool(nodes=4) as pool:
    results = pool.map(lambda x: x * x, range(10))

Using Map Decorators

The package provides three decorators for different mapping strategies:

from twat_mp import amap, imap, pmap

# Standard parallel map (eager evaluation)
@pmap
def square(x: int) -> int:
    return x * x

results = list(square(range(10)))
print(results)  # [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

# Lazy parallel map (returns iterator)
@imap
def cube(x: int) -> int:
    return x * x * x

for result in cube(range(5)):
    print(result)  # Prints results as they become available

# Asynchronous parallel map with automatic result retrieval
@amap
def double(x: int) -> int:
    return x * 2

results = list(double(range(10)))
print(results)  # [0, 2, 4, 6, 8, 10, 12, 14, 16, 18]

Function Composition

Decorators can be composed for complex parallel operations:

from twat_mp import amap

@amap
def compute_intensive(x: int) -> int:
    result = x
    for _ in range(1000):  # Simulate CPU-intensive work
        result = (result * x + x) % 10000
    return result

@amap
def io_intensive(x: int) -> int:
    import time
    time.sleep(0.001)  # Simulate I/O wait
    return x * 2

# Chain parallel operations
results = list(io_intensive(compute_intensive(range(100))))

Dependencies

  • pathos: For parallel processing functionality

Development

To set up the development environment:

# Install in development mode with test dependencies
uv pip install -e ".[test]"

# Run tests
python -m pytest tests/

# Run benchmarks
python -m pytest tests/test_benchmark.py

License

MIT License

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

twat_mp-1.6.2.tar.gz (72.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

twat_mp-1.6.2-py3-none-any.whl (6.5 kB view details)

Uploaded Python 3

File details

Details for the file twat_mp-1.6.2.tar.gz.

File metadata

  • Download URL: twat_mp-1.6.2.tar.gz
  • Upload date:
  • Size: 72.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for twat_mp-1.6.2.tar.gz
Algorithm Hash digest
SHA256 b69dd730cc291d3868c1edc84b939c54669f0f3ce524110ac4a577ccafce5a62
MD5 210fc9ada7faec0b8992017fee1e5a13
BLAKE2b-256 a468d8603f858c0f0c87e98abec3ee2b7bc7b6632ff19bf1783aeadc7e44239e

See more details on using hashes here.

File details

Details for the file twat_mp-1.6.2-py3-none-any.whl.

File metadata

  • Download URL: twat_mp-1.6.2-py3-none-any.whl
  • Upload date:
  • Size: 6.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for twat_mp-1.6.2-py3-none-any.whl
Algorithm Hash digest
SHA256 b2bdf39b2e4322d35191dc1db6467c4bef301558ef769eadb9ce0551818301a7
MD5 987ded48cf92ebd0cd336b6fbe594281
BLAKE2b-256 d86d3b60891758432ad275329c84c1ef662ae65c316ad9771e5c40602a0648e2

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page