Created
July 9, 2016 14:45
-
-
Save tokibito/5b36eae0f2fdedb0ebefcc3a1181858b to your computer and use it in GitHub Desktop.
Example for concurrent.futures
This file contains hidden or 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
import os | |
import threading | |
from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor | |
from time import sleep | |
def writer(value): | |
sleep(1) | |
print("PID={}, ThreadID={}: {}".format(os.getpid(), threading.get_ident(), value)) | |
def main(): | |
# executor_class = ThreadPoolExecutor | |
executor_class = ProcessPoolExecutor | |
executor = executor_class(max_workers=3) | |
tasks = [] | |
print("--start--") | |
for i in range(10): | |
tasks.append(executor.submit(writer, i)) | |
while not all([t.done() for t in tasks]): | |
sleep(0.1) | |
print("--done--") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment