Created
July 10, 2019 15:34
-
-
Save nijave/2b20504902296da03b19adcfdb7bc490 to your computer and use it in GitHub Desktop.
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 import futures | |
import collections | |
import time | |
def job(job_id): | |
time.sleep(job_id) | |
return job_id | |
def example1(): | |
""" | |
Submits jobs to a ThreadPool | |
:return: | |
""" | |
executor = futures.ThreadPoolExecutor(max_workers=32) | |
results = collections.deque() | |
for i in range(100): | |
result = executor.submit(job, i) | |
results.append(result) | |
print([result.result() for result in results]) | |
def example2(): | |
""" | |
Submits jobs to a ProcessPool | |
:return: | |
""" | |
executor = futures.ProcessPoolExecutor(max_workers=32) | |
results = collections.deque() | |
for i in range(100): | |
result = executor.submit(job, i) | |
results.append(result) | |
print([result.result() for result in results]) | |
def example3(): | |
""" | |
Maps jobs to a ThreadPoolExecutor | |
:return: | |
""" | |
executor = futures.ThreadPoolExecutor(max_workers=32) | |
results = executor.map(job, range(100)) | |
print([result.result for result in results]) | |
def example4(): | |
""" | |
Maps jobs to a ProcessPoolExecutor | |
:return: | |
""" | |
executor = futures.ProcessPoolExecutor(max_workers=32) | |
results = executor.map(job, range(100)) | |
print([result.result for result in results]) | |
if __name__ == "__main__": | |
examples = [ | |
"example1", | |
"example2", | |
"example3", | |
"example4", | |
] | |
for ex in examples: | |
start = time.time() | |
print("Running %s" % ex) | |
locals()[ex]() | |
print("%s ran in %f seconds" % (ex, time.time()-start)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment