Skip to content

Instantly share code, notes, and snippets.

@duskvirkus
Created October 27, 2022 18:06
Show Gist options
  • Save duskvirkus/0fa6dd15451883dc03a11683c814f2e7 to your computer and use it in GitHub Desktop.
Save duskvirkus/0fa6dd15451883dc03a11683c814f2e7 to your computer and use it in GitHub Desktop.
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment