-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_3_3_module_concurrent.futures_example.py
45 lines (35 loc) · 1.34 KB
/
_3_3_module_concurrent.futures_example.py
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
# Script (3.3_module_concurrent.futures_example.py):
# A demonstration of parallel processing using `concurrent.futures` in Python.
# This script utilizes `ProcessPoolExecutor` for efficient computation of factorials.
#
# Prerequisites:
# - Python 3.12 or higher.
#
# Paper: Parallel Processing – An In-Depth Look Into Python 3.13 (2025)
# Authors: Mantvydas Deltuva and Justinas Teselis
from concurrent.futures import ProcessPoolExecutor
from multiprocessing import current_process
import random
import time
import math
# Universal function to compute factorial of a number
def compute_factorial(number):
# Provide start feedback
process_name = current_process().name
sleep_time = random.uniform(0.5, 2.0)
print(
f"Process {process_name} is calculating factorial "
+ f"for {number} with sleep time {sleep_time}"
)
# Simulate a heavy computation with a random sleep time
time.sleep(sleep_time)
# Return the factorial of the number
return math.factorial(number)
if __name__ == "__main__":
# Numbers to calculate the factorial of
numbers = [1, 16, 4, 5, 8, 6, 2]
# Create ProcessPoolExecutor for parallel execution
with ProcessPoolExecutor(max_workers=4) as executor:
results = list(executor.map(compute_factorial, numbers))
# Provide finish feedback
print(f"Results: {results}")