How to use concurrent.futures for parallel execution in Python?

98 views

How to use concurrent.futures for parallel execution in Python?

How to use concurrent.futures for parallel execution in Python?

solveurit24@gmail.com Changed status to publish February 13, 2025
0

Utilize ThreadPoolExecutor or ProcessPoolExecutor.

from concurrent.futures import ThreadPoolExecutor

def process(i):
    return i * i

with ThreadPoolExecutor(max_workers=4) as executor:
    futures = [executor.submit(process, i) for i in range(10)]
    for future in concurrent.futures.as_completed(futures):
        print(future.result())

Output:

0 to 81 printed asynchronously
solveurit24@gmail.com Changed status to publish February 13, 2025
0
You are viewing 1 out of 1 answers, click here to view all answers.