Simplify use of ray
Project description
Ray Simplify
A Python package that simplifies the use of Ray for parallel execution by providing an intuitive, context-manager-based API with automatic resource management.
Overview
Ray Simplify abstracts away the complexity of Ray's distributed computing framework, allowing you to easily parallelize your Python functions with minimal code changes. It automatically handles Ray initialization, worker management, and cleanup, so you can focus on your core logic.
Why Ray Simplify?
Addresses Real Pain Points
Ray's initialization and resource management can be complex and error-prone. Ray Simplify genuinely simplifies common use cases by providing a clean, Pythonic interface that follows established patterns.
Compare the Complexity:
Without Ray Simplify (Complex):
import ray
# Manual Ray management
ray.init(num_cpus=4, log_to_driver=True)
try:
@ray.remote
def process_item(x):
return x * x
# Manual future management
futures = [process_item.remote(i) for i in range(100)]
results = ray.get(futures)
# Manual cleanup and error handling
finally:
ray.shutdown()
With Ray Simplify (Simple):
from ray_simplify import parallel, parallel_map, parallelize
@parallelize()
def process_item(x):
return x * x
# Automatic Ray management with context manager
with parallel(num_cpus=4):
results = list(parallel_map(process_item, range(100)))
Production-Ready Quality
- Comprehensive Error Handling: Custom exceptions for different failure scenarios
- Resource Safety: Automatic cleanup prevents Ray cluster leaks
- Type Safety: Full type annotations for better IDE support
- Battle-Tested: 100% test coverage with extensive edge case handling
- Modern Standards: Follows current Python packaging and development best practices
Market Position
Ray Simplify fills the same role for distributed computing that successful packages like requests (HTTP), click (CLI), and pathlib (file operations) filled for their domains: taking powerful but complex tools and making them accessible with clean, intuitive APIs.
Target Audience:
- Data scientists who need Ray's distributed power without the complexity
- Python developers doing CPU-intensive computational work
- Teams wanting reliable parallel processing with minimal setup and maintenance
- Organizations that need Ray's scalability but want to reduce development overhead
Features
- Simple Context Manager: Use
with parallel():to create a parallel execution environment - Function Decorator: Apply
@parallelize()to make any function parallel-ready - Automatic Resource Management: Ray cluster is automatically started and stopped
- Type Safety: Full type annotations for better IDE support and code reliability
- Comprehensive Error Handling: Specific exception types for different failure scenarios
- Flexible Configuration: Customize CPU allocation, logging, and distributed execution settings
Installation
pip install ray_simplify
Quick Start
Basic Usage with Context Manager
from ray_simplify import parallel, parallel_map, parallelize
import time
@parallelize()
def square(x):
"""Calculate the square of a number with simulated work."""
time.sleep(1) # Simulate computation
return x * x
# Sequential execution (slow)
def sequential_example():
numbers = [1, 2, 3, 4, 5]
results = [square(x) for x in numbers] # Takes ~5 seconds
return results
# Parallel execution (fast)
def parallel_example():
numbers = [1, 2, 3, 4, 5]
with parallel():
results = list(parallel_map(square, numbers)) # Takes ~1 second
return results
Advanced Usage
from ray_simplify import (
parallel,
parallel_map,
parallel_shared,
parallel_iter,
parallel_wait,
parallelize
)
@parallelize()
def process_data(item, shared_config):
"""Process an item using shared configuration."""
# Access shared data efficiently
config = ray.get(shared_config)
return item * config['multiplier']
def advanced_example():
# Configure parallel context
with parallel(num_cpus=4, log_level="DEBUG") as ctx:
# Share large data across workers efficiently
config = {"multiplier": 10, "threshold": 100}
shared_config = parallel_shared(config)
# Process data in parallel
items = list(range(1000))
results = parallel_map(
lambda x: process_data(x, shared_config),
items
)
# Stream results as they complete
for result in parallel_iter(results):
if result > config['threshold']:
print(f"High value result: {result}")
API Reference
Context Manager
parallel(**kwargs)
Creates a parallel execution context with automatic Ray management.
Parameters:
num_cpus(int): Number of CPUs to allocate (default: 8)on_host(str): Remote host address for distributed executionlog_level(str): Ray logging level (default: "INFO")log_to_driver(bool): Whether to log to driver (default: True)
Decorators
@parallelize(max_concurrency=None)
Decorator that enables a function to run in parallel within a parallel context.
Parameters:
max_concurrency(int, optional): Maximum concurrent tasks
Parallel Functions
parallel_map(func, iterable, timeout=None)
Apply a function to an iterable in parallel.
parallel_shared(data)
Share data efficiently across Ray workers.
parallel_iter(futures)
Iterate over futures as results become available.
parallel_wait(future, timeout=None)
Wait for a single future to complete.
Exception Classes
RayError: Base exception for Ray-related errorsRayInitializationError: Raised when Ray fails to initializeRayProcessError: Raised when Ray subprocess operations fail
Performance Example
Here's a real example showing the performance improvement:
import time
import timeit
from ray_simplify import parallel, parallel_map, parallelize
@parallelize()
def cpu_bound_task(n):
"""Simulate CPU-intensive work."""
time.sleep(2) # Simulate 2 seconds of work
return n * n
def compare_performance():
numbers = list(range(10)) # 10 tasks, each taking 2 seconds
# Sequential execution
start = timeit.default_timer()
sequential_results = [cpu_bound_task(n) for n in numbers]
sequential_time = timeit.default_timer() - start
print(f"Sequential: {sequential_time:.2f} seconds") # ~20 seconds
# Parallel execution
start = timeit.default_timer()
with parallel():
parallel_results = list(parallel_map(cpu_bound_task, numbers))
parallel_time = timeit.default_timer() - start
print(f"Parallel: {parallel_time:.2f} seconds") # ~2-4 seconds
print(f"Speedup: {sequential_time / parallel_time:.2f}x")
if __name__ == "__main__":
compare_performance()
Requirements
- Python 3.11+
- Ray (automatically installed)
Development
This project follows modern Python development practices:
- Code Quality: Black formatting, Ruff linting, pre-commit hooks
- Testing: Pytest with comprehensive test coverage
- Type Safety: Full type annotations with mypy checking
- Documentation: Google-style docstrings with examples
- CI/CD: Automated testing and releases
Setting up Development Environment
# Clone the repository
git clone <repository-url>
cd ray_simplify
# Install development dependencies
make install
# Run tests
make test
# Run code quality checks
make lint
# Run all checks
make check
Architecture
Ray Simplify is organized into focused modules:
context.py: Parallel context manager with automatic Ray lifecycle managementdecorators.py: Function decorators for parallel executionexecutor.py: Core Ray execution operationsfunctions.py: Public API functionsprocess_manager.py: Ray process and cluster managementexceptions.py: Custom exception classesconstants.py: Package constants
License
This project is licensed under the MIT License - see the LICENSE file for details.
Contributing
Contributions are welcome! Please see CONTRIBUTE.md for guidelines.
Authors
- Dinhduy Tran dinhduy.tran@live.com
Credits
This project was developed with assistance from GitHub Copilot powered by Claude Sonnet 4. The AI assistant helped ensure this package follows current industry standards and provides a production-ready solution for simplified Ray usage.
Note: Ray Simplify is designed to make parallel computing accessible to Python developers without requiring deep knowledge of distributed systems. It handles the complexity so you can focus on your application logic.
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 ray_simplify-1.0.0.tar.gz.
File metadata
- Download URL: ray_simplify-1.0.0.tar.gz
- Upload date:
- Size: 81.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d88b923e6e93d018e499056ff81d1f685cb4e41394adbd9bb5b93d37dd19079d
|
|
| MD5 |
950d2c5bdcf4786b54ba9695a8bd927d
|
|
| BLAKE2b-256 |
16fe85266eff945094555e238a63a21c3f4edbe98036009703baabcd112a7aff
|
File details
Details for the file ray_simplify-1.0.0-py3-none-any.whl.
File metadata
- Download URL: ray_simplify-1.0.0-py3-none-any.whl
- Upload date:
- Size: 18.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
673e8f2f39e4e863a9aedbbfe54e9e83ef9e24121023f654eeaa9cf233e6a08c
|
|
| MD5 |
2396c38dc3c55f1c995465bc7349c90c
|
|
| BLAKE2b-256 |
52ddddba46564f7aa76f57881d67cdb36975eafeeb1f48bf09f5fc32c980bf7c
|