Is it ok to use ThreadPoolExecutor for single Thread script?

Viewed 274

I have a script that I want to execute it in single or multi-thread based on the user specification in the command-line.

In this case I am thinking to implement it like this:

from concurrent import futures
import sys
import time


def print_hello(input):
    time.sleep(1.0)
    print("{0}: {1} Hello world!".format(int(time.time()), input))


def main(workers):

    pool = futures.ThreadPoolExecutor(max_workers=workers)

    uploads = []

    for greet in range(10):
        upload = pool.submit(print_hello, greet)
        uploads.append(upload)

    futures.wait(uploads)


if __name__ == "__main__":

    if len(sys.argv) > 1:
        num = sys.argv[1]
    else:
        num = 1

    main(int(num))

i.e an execution with more than 5 workers

python parallel.py 5

will run ok, BUT in case that the execution is a single thread i.e

python parallel.py

will it be any drawback to run my single thread script using

futures.ThreadPoolExecutor(max_workers=1)

Do you suggest any other Pythonic way to execute this script by switching single/parallel based on user specification?

Thank you

0 Answers
Related