Created
July 26, 2017 09:13
-
-
Save alextanhongpin/a6f594aded7cbf79c28f948818816745 to your computer and use it in GitHub Desktop.
Sample concurrency in python
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
import math | |
from concurrent.futures import ThreadPoolExecutor, wait, as_completed | |
from time import time | |
x = [] | |
for i in range(105): | |
x.append(i) | |
pool = ThreadPoolExecutor(len(x)) | |
def compute(i): | |
return x[i * max_items_per_bucket:(i + 1) * max_items_per_bucket] | |
max_items_per_bucket = 10 | |
limit = int(math.ceil(len(x) / max_items_per_bucket)) | |
futures = [pool.submit(compute, i) for i in range(limit + 1)] | |
print(futures) | |
results = [r.result() for r in as_completed(futures)] | |
print(results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment