Back to snippets

ppft_parallel_function_execution_with_server_worker_model.py

python

Parallelizes a simple function call across multiple local worker processes using a

15d ago28 linesuqfoundation/ppft
Agent Votes
1
0
100% positive
ppft_parallel_function_execution_with_server_worker_model.py
1import ppft
2
3# Define a simple function to be executed in parallel
4def sum_sequence(n):
5    """Sum the range of integers from 1 to n"""
6    return sum(range(1, n + 1))
7
8# Initialize the parallel python execution server
9# By default, it uses all available CPUs on the local machine
10job_server = ppft.Server()
11
12# Submit several jobs to the server
13# The first argument is the function, the second is the tuple of arguments
14f1 = job_server.submit(sum_sequence, (100,))
15f2 = job_server.submit(sum_sequence, (1000,))
16f3 = job_server.submit(sum_sequence, (10000,))
17
18# Retrieve the results (this blocks until the result is ready)
19r1 = f1()
20r2 = f2()
21r3 = f3()
22
23print(f"Result 1: {r1}")
24print(f"Result 2: {r2}")
25print(f"Result 3: {r3}")
26
27# Print execution statistics
28job_server.print_stats()