Created
March 8, 2022 23:11
-
-
Save jonchang/324eb93b08b365a212233fb800ea1f3e to your computer and use it in GitHub Desktop.
concurrent.futures example
This file contains 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
# concurrent.futures example | |
# - submits work to task pool asynchronously | |
# - post-process results as they come in | |
from concurrent.futures import ProcessPoolExecutor, as_completed | |
from time import sleep | |
from random import randint | |
def do_some_work(item): | |
print(f"Doing some work on: {item}") | |
sleep(randint(1, 5)) | |
return item | |
if __name__ == '__main__': | |
replicates = 5 | |
results = [] | |
with ProcessPoolExecutor() as executor: | |
futures = [] | |
for idx in range(replicates): | |
futures.append(executor.submit(do_some_work, idx)) | |
for future in as_completed(futures): | |
result = future.result() | |
results.append(result) | |
print(f"A result streamed in: {result}") | |
# Returned in random order | |
print(f"All my results: {results}") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment