Skip to content

Instantly share code, notes, and snippets.

@wilk
Last active August 17, 2018 14:44
Show Gist options
  • Save wilk/365490b017c2713aa1280f20af1b1bec to your computer and use it in GitHub Desktop.
Save wilk/365490b017c2713aa1280f20af1b1bec to your computer and use it in GitHub Desktop.
Multithreading execution with ThreadPoolExecutor
# async tests
from time import sleep
from concurrent.futures import ThreadPoolExecutor, wait
def hello_world(delay):
print("hello world {delay}".format(delay=delay))
sleep(delay)
print("hello there {delay}".format(delay=delay))
return "done {delay}".format(delay=delay)
executor = ThreadPoolExecutor(max_workers=8)
futures = {
executor.submit(hello_world, 1): 1,
executor.submit(hello_world, 2): 2,
executor.submit(hello_world, 3): 3,
executor.submit(hello_world, 4): 4,
executor.submit(hello_world, 1): 5,
executor.submit(hello_world, 2): 6,
executor.submit(hello_world, 3): 7,
executor.submit(hello_world, 4): 8
}
completed, _ = wait(futures)
for future in completed:
print(futures[future])
print(future.result())
print("EEEEEEND")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment