parallelizing jobs in python
import time
from concurrent.futures import ThreadPoolExecutor, wait
import numpy as np
def work(aa, bb):
time.sleep(np.random.rand())
print(f"working on {aa}, {bb}")
return aa + bb
with ThreadPoolExecutor(8) as executor:
# futures = []
# for ii in range(33):
# futures.append(
# executor.submit(work, ii, ii+1)
# )
futures = [executor.submit(work, ii, ii+1) for ii in range(33)]
wait(futures)
print("++++++++++++++++++++++++++++++")
for fut in futures:
print(fut.result())
print("all done.")