在python中使用concurrent.futures实现进程池和线程池

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import concurrent.futures
import time

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

def evaluate_item(x):
    result_item = count(x)
    print("item " + str(x) + " result " + str(result_item))

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

if __name__ == "__main__":
    start_time = time.clock()
    for item in number_list:
        evaluate_item(item)
    print("Sequential execution in " +
          str(time.clock() - start_time) + " seconds")

    start_time_1 = time.clock()
    with concurrent.futures.ThreadPoolExecutor(max_workers=5)\
         as executor:
        for item in number_list:
            executor.submit(evaluate_item, item)

    print("Thread pool execution in " + \
          str(time.clock() - start_time_1) + " seconds")

    start_time_2 = time.clock()
    with concurrent.futures.ProcessPoolExecutor(max_workers=5) \
         as executor:
        for item in number_list:
            executor.submit(evaluate_item, item)
    print("Process pool execution in " + \
          str(time.clock() - start_time_2) + " seconds")
    

在python中使用concurrent.futures实现进程池和线程池

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-14
  • 2022-12-23
  • 2022-01-23
  • 2022-01-04
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-07-03
  • 2021-09-01
  • 2022-12-23
  • 2022-02-14
相关资源
相似解决方案