Last active
August 17, 2018 14:44
-
-
Save wilk/365490b017c2713aa1280f20af1b1bec to your computer and use it in GitHub Desktop.
Multithreading execution with ThreadPoolExecutor
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
# 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