Skip to content

Instantly share code, notes, and snippets.

@twillis
Last active January 2, 2016 09:29
Show Gist options
  • Save twillis/8283111 to your computer and use it in GitHub Desktop.
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.
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