Skip to content

Instantly share code, notes, and snippets.

@alextanhongpin
Created July 26, 2017 09:13
Show Gist options
  • Save alextanhongpin/a6f594aded7cbf79c28f948818816745 to your computer and use it in GitHub Desktop.
Save alextanhongpin/a6f594aded7cbf79c28f948818816745 to your computer and use it in GitHub Desktop.
Sample concurrency in python
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