-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathparallel_processing_example.py
More file actions
executable file
·70 lines (54 loc) · 2.27 KB
/
Copy pathparallel_processing_example.py
File metadata and controls
executable file
·70 lines (54 loc) · 2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""
Parallel Processing Example
This example demonstrates how to use the parallel_map function
to process items in parallel using multiple worker processes.
"""
import time
from typing import List, Dict, Any
from ellma.modules import parallel_map
def process_image(image_path: str) -> Dict[str, Any]:
"""Simulate processing an image (CPU-bound operation)."""
print(f"Processing {image_path}...")
time.sleep(0.5) # Simulate processing time
# Simulate some image processing
width = 1000
height = 800
file_size = len(image_path) * 1000 # Fake file size
return {
'path': image_path,
'dimensions': (width, height),
'size_kb': file_size // 1024,
'processed_at': time.time()
}
def process_images_sequentially(image_paths: List[str]) -> List[Dict[str, Any]]:
"""Process images one at a time (sequential)."""
print("\nProcessing images sequentially...")
start_time = time.time()
results = []
for path in image_paths:
results.append(process_image(path))
elapsed = time.time() - start_time
print(f"Processed {len(results)} images in {elapsed:.2f} seconds")
return results
def process_images_in_parallel(image_paths: List[str], max_workers: int = 4) -> List[Dict[str, Any]]:
"""Process images in parallel using parallel_map."""
print(f"\nProcessing images in parallel (max_workers={max_workers})...")
start_time = time.time()
results = parallel_map(process_image, image_paths, max_workers=max_workers)
elapsed = time.time() - start_time
print(f"Processed {len(results)} images in {elapsed:.2f} seconds")
return results
def main():
print("=== Parallel Processing Example ===\n")
# Generate some sample image paths
image_paths = [f"images/photo_{i}.jpg" for i in range(1, 9)]
# Process images sequentially
seq_results = process_images_sequentially(image_paths)
# Process images in parallel with different worker counts
for workers in [2, 4, 8]:
parallel_results = process_images_in_parallel(image_paths, max_workers=workers)
# Verify all results are the same
assert len(seq_results) == len(parallel_results)
print("\n✅ All images processed successfully!")
if __name__ == "__main__":
main()