Python线程池ThreadPoolExecutor简单使用

from datetime import datetime
from concurrent.futures import ThreadPoolExecutor, as_completed


def quick_sort(lst):
    """
    快速排序
    :param lst:
    :return:
    """
    if len(lst) < 2:
        return lst
    pivot = lst[0]
    less = [i for i in lst[1:] if i <= pivot]
    greater = [i for i in lst[1:] if i > pivot]
    return quick_sort(less) + [pivot] + quick_sort(greater), "排序完成"


lst = [5, 6, 8, 7, 2, 1, 9, 4, 3, 0]
start_time = datetime.now()
with ThreadPoolExecutor(max_workers=4) as executor:
    future_list = list()
    for i in range(16):
        future = executor.submit(quick_sort, lst)
        print(11111111, future)
        future_list.append(future)
    for res in as_completed(future_list):
        # a, b = res.result()
        print(22222222, res)

end_time = datetime.now()
print(f"共耗时{end_time - start_time}")

posted @ 2021-10-11 11:37  嗨,阿良  阅读(493)  评论(0编辑  收藏  举报