How to use ThreadPoolExecutor for parallel execution?

95 views

How to use ThreadPoolExecutor for parallel execution?

How to use ThreadPoolExecutor for parallel execution?

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

Utilize ThreadPoolExecutor to manage threads and execute tasks concurrently.

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:

Random order of 0 to 81
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.