Home

Published

- 1 min read

threadpoolexecutor python example

img of threadpoolexecutor python example

The solution for this is noted below

threadpoolexecutor python example

Solution

   from time import sleep, perf_counter
from concurrent.futures import ThreadPoolExecutor

def task(id):
    print(f'Starting the task {id}...')
    sleep(1)
    return f'Done with task {id}'

start = perf_counter()

with ThreadPoolExecutor() as executor:
    f1 = executor.submit(task, 1)
    f2 = executor.submit(task, 2)

    print(f1.result())
    print(f2.result())

finish = perf_counter()

print(f"It took {finish-start} second(s) to finish.")
Code language: Python (python)

Try other methods by searching on the site. That is if this doesn’t work