Search code examples
pythonmultithreadingconcurrent.futures

How to run concurrently using ThreadPoolExecutor


I have been working for a while with Threading and I managed to find out that I was not able to run concurrently when i'm using threading. Here is a example:

import concurrent.futures
import time
import random
from typing import List

site_catalog: List[str] = ["Thrill", "Test"]


def parse_value(value: str) -> None:
    while True:
        print("Caught: ", value)
        time.sleep(random.randint(3, 5))


with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    for get_values in site_catalog:
        executor.submit(parse_value(value=get_values))

>>> Caught:  Thrill
Caught:  Thrill
Caught:  Thrill
Caught:  Thrill
Caught:  Thrill
Caught:  Thrill
Caught:  Thrill
Caught:  Thrill
Caught:  Thrill
Caught:  Thrill
Caught:  Thrill
Caught:  Thrill
Caught:  Thrill
Caught:  Thrill

I wonder how can I run the values from the List site_catalogconcurrently? Meaning the output would be something like:

>>> Caught:  Thrill
Caught:  Test
Caught:  Thrill
Caught:  Test
Caught:  Test
Caught:  Thrill
Caught:  Test
Caught:  Thrill
Caught:  Thrill
Caught:  Test
Caught:  Thrill

That they are not dependent on each other's execution


Solution

  • It was as easy as replace it to:

    executor.submit(parse_value(value=get_values))
    

    to:

    executor.submit(parse_value, get_values)