import click @click.command() @click.option('-i', '--input', 'opt_fp_in', required=True, help='Input') @click.option('-o', '--output', 'opt_fp_out', required=True, help='Output') @click.option('-t', '--threads', 'opt_threads', default=4, help='Number of threads') @click.pass_context def cli(ctx, opt_fp_in, opt_fp_out, opt_threads): """Template for multithreading""" from functools import partial from multiprocessing.dummy import Pool as ThreadPool from tqdm import tqdm from app.utils.logger_utils import Logger log = Logger.getLogger() log.info('multithreaded template') # setup multithreading function def pool_process(item): # threaded function results = [] try: # do something here with item except Exception as e: log.debug(f'Error: {e}') pbar.update(1) return results # setup multithreading data holds pool_items = [] # list of dicts to process pool_results = [] num_items = len(pool_items) # run the multithreading with progress bar pbar = tqdm(total=num_items) pool_process = partial(pool_process) pool = ThreadPool(opt_threads) with tqdm(total=num_items) as pbar: pool_results = pool.map(pool_process, pool_items) pbar.close()