Last active
January 2, 2016 09:29
-
-
Save twillis/8283111 to your computer and use it in GitHub Desktop.
example of using concurrent.futures map and as completed to submit a bunch of "jobs" and deal with the results as they complete.
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
from concurrent.futures import as_completed, ThreadPoolExecutor | |
import random | |
import time | |
def do_some_work(work_input): | |
result = random.choice(range(1,2)) | |
print("sleeping for %s (%s)" % (result, work_input)) | |
time.sleep(result) | |
return (result, work_input) | |
with ThreadPoolExecutor(8) as ex: | |
for f in as_completed([ex.submit(do_some_work, x) for x in range(1, 100)]): | |
print(f.result()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment