concurrent.futures实现多进程多线程

python的concurrent.futures模块实现进程池线程池,继而实现多进程多线程
参考python的concurrent.futures模块
1、

# -*- coding: utf-8 -*-
import time
import concurrent.futures


number_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def evaluate_item(x):
    # 计算总和,这里只是为了消耗时间
    result_item = count(x)
    return result_item

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

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

        # 线程池
        start_time_t = time.time()
        with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
            futures = [executor.submit(evaluate_item, item) for item in number_list]
            for future in concurrent.futures.as_completed(futures):
                print(future.result())
        print ("Thread pool execution in " + str(time.time() - start_time_t), "seconds")

        # 进程池
        start_time_p = time.time()
        with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executor:
            futures = [executor.submit(evaluate_item, item) for item in number_list]
            for future in concurrent.futures.as_completed(futures):
                print(future.result())
        print ("Process pool execution in " + str(time.time() - start_time_p), "seconds")

2、

# -*- coding: utf-8 -*-
import time
import concurrent.futures


number_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

def evaluate_item(x):
    # 计算总和,这里只是为了消耗时间
    result_item = count(x)
    return result_item

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

if __name__ == "__main__":

        # 线程池
        start_time_t = time.time()
        with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
            for number, prime in zip(number_list, executor.map(evaluate_item, number_list)):
                print('%d is prime: %s' % (number, prime))
        print ("Thread pool execution in " + str(time.time() - start_time_t), "seconds")

        # 进程池
        start_time_p = time.time()
        with concurrent.futures.ProcessPoolExecutor(max_workers=5) as executor:
            for number, prime in zip(number_list, executor.map(evaluate_item, number_list)):
                print('%d is prime: %s' % (number, prime))
        print ("Process pool execution in " + str(time.time() - start_time_p), "seconds")

你可能感兴趣的:(Python,python3多进程多线程,concurrent,futures)