python 线程池和进程池(draft)

from concurrent import futures
from multiprocessing import cpu_count

import time

number_list = range(0, 40)


def evaluate_item(x):
    # 计算总和,这里只是为了消耗时间
    result_item = count(x)
    # 打印输入和输出结果
    return result_item


def i_o_function(x):
    time.sleep(10)


def count(number):
    for i in range(0, 100000000):
        i = i + 1
    return i * number


if __name__ == "__main__":
    # 顺序执行
    # start_time = time.time()
    # for item in number_list:
    #         print(i_o_function(item))
    # print("Sequential execution in " + str(time.time() - start_time), "seconds")

    # 进程池执行

    # print ("ProcessPoolExecutor workers=3")
    # start_time1 = time.time()
    # with futures.ProcessPoolExecutor(max_workers=3) as executor:
    #   future_list = [executor.submit(evaluate_item, item) for item in number_list]
    #   for future in futures.as_completed(future_list):
    #       print (future.result())
    # print ("Process pool executuin in " + str(time.time() - start_time1) + "seconds")

    # print ("ProcessPoolExecutor workers=5")
    # start_time1 = time.time()
    # with futures.ProcessPoolExecutor(max_workers=5) as executor:
    #   future_list = [executor.submit(evaluate_item, item) for item in number_list]
    #   for future in futures.as_completed(future_list):
    #       print (future.result())
    # print ("Process pool executuin in " + str(time.time() - start_time1) + "seconds")

    # print ("ProcessPoolExecutor workers=10")
    # start_time1 = time.time()
    # with futures.ProcessPoolExecutor(max_workers=10) as executor:
    #   future_list = [executor.submit(evaluate_item, item) for item in number_list]
    #   for future in futures.as_completed(future_list):
    #       print (future.result())
    # print ("Process pool executuin in " + str(time.time() - start_time1) + "seconds")

    # print ("ProcessPoolExecutor workers=%s" % cpu_count())
    # start_time1 = time.time()
    # with futures.ProcessPoolExecutor() as executor:
    #   future_list = [executor.submit(i_o_function, item) for item in number_list]
    #   for future in futures.as_completed(future_list):
    #       print (future.result())
    # print ("Process pool executuin in " + str(time.time() - start_time1) + "seconds")

    # 线程池执行
    start_time2 = time.time()
    with futures.ThreadPoolExecutor() as executor:
        future_list = [executor.submit(i_o_function, item) for item in number_list]
        for future in futures.as_completed(future_list):
            print(future.result())
    print("Thread pool executuin in " + str(time.time() - start_time2) + "seconds")

    start_time2 = time.time()
    with futures.ThreadPoolExecutor(max_workers=100000) as executor:
        future_list = [executor.submit(i_o_function, item) for item in number_list]
        for future in futures.as_completed(future_list):
            future.result()
    print("Thread pool executuin in " + str(time.time() - start_time2) + "seconds")

你可能感兴趣的:(python 线程池和进程池(draft))