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.")

posted on 2023-05-24 01:05  yusisc  阅读(5)  评论(0编辑  收藏  举报

导航