Search code examples
pythonpython-3.xmultithreadingthreadpoolpython-multithreading

Python ThreadPoolExecutor not running parallelly


Python ThreadPoolExecutor not running parallelly, it is calling cube method in sequence and waiting for complete,

where I need to run 20 parallell threads

from concurrent.futures import ThreadPoolExecutor
from time import sleep

def cube(x):
    sleep(2)
    print(f'Cube of {x}:{x*x*x}')

count = 0
while True:
    with ThreadPoolExecutor(max_workers=20) as exe:
        exe.submit(cube,2)
    count += 1
    if count > 50:
        break   

Solution

  • "with" statement uses __enter__ method for ThreadPoolExecutor, it initialize thread pool at this moment. So, for function execution in thread pool we should use it inside "with" statement.

    with ThreadPoolExecutor(max_workers=20) as exe:  # for default it will be number of os.cpu_count()
        futures = []
        for i in range(50):
            futures.append(executor.submit(cube, i))
        for future in concurrent.futures.as_completed(futures):
            print(future.result())