Created
August 8, 2024 11:48
-
-
Save palawer/fdc5f4742147efd153b41dc283a76c32 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
from concurrent.futures import ThreadPoolExecutor, as_completed | |
from itertools import zip_longest | |
def grouper(iterable, n=1000, fillvalue=None): | |
args = [iter(iterable)] * n | |
return zip_longest(*args, fillvalue=fillvalue) | |
def async_func(number): | |
return number * number | |
my_list = [1, 2, 3] | |
with ThreadPoolExecutor(max_workers=8) as executor: | |
for chunk in grouper(my_list): | |
futures = {} | |
for number in chunk: | |
if not number: continue # skip | |
futures[executor.submit(async_func, number)] = number | |
for future in as_completed(futures): | |
number = futures[future] | |
try: | |
result = future.result() | |
print(f'{number}: {result}') | |
except Exception as e: | |
print(e) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment