Skip to content

Instantly share code, notes, and snippets.

@duskvirkus
Created October 27, 2022 18:06
Show Gist options
  • Select an option

  • Save duskvirkus/0fa6dd15451883dc03a11683c814f2e7 to your computer and use it in GitHub Desktop.

Select an option

Save duskvirkus/0fa6dd15451883dc03a11683c814f2e7 to your computer and use it in GitHub Desktop.

Revisions

  1. duskvirkus created this gist Oct 27, 2022.
    28 changes: 28 additions & 0 deletions multi_thread.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,28 @@
    import concurrent.futures

    from tqdm import tqdm

    import random
    import time

    def run_on_thread_example(packed_args):
    arg_one, arg_two = packed_args

    time.sleep(int(random.random() * 2))
    result = arg_one * arg_two
    print(f"arg_one: {arg_one}, arg_two: {arg_two:.2f}, result: {result:.2f}")

    return {
    "index": arg_one,
    "result": result,
    }

    if __name__ == "__main__":

    arg_list_one = [i for i in range(50)]
    arg_list_two = [random.random() for _ in range(len(arg_list_one))]

    with concurrent.futures.ThreadPoolExecutor() as executor:
    results = list(tqdm(executor.map(run_on_thread_example, zip(arg_list_one, arg_list_two)), total=len(arg_list_one)))

    print(results)