当前位置:   article > 正文

python学习笔记之多进程练习multiprocessing对CPU密集型任务进行优化_python multiprocessing现状 cpu

python multiprocessing现状 cpu

主要练习使用多进程ProcessPoolExecutor对CPU密集型任务进行优化

import time
from multiprocessing import Process, Queue, Lock
from concurrent.futures import ProcessPoolExecutor


def func(x):
    '''
    CPU密集型任务
    单个耗时2.94 seconds
    '''
    for i in range(50000000):
        x += i
    return x


if __name__ == '__main__':
    t0 = time.time()
    with ProcessPoolExecutor() as executor:
        results = executor.map(func, [1, 2, 3])
        for result in results:
            print(result)
    t1 = time.time()
    print("ProcessPoolExecutor多进程运行cost:", t1 - t0, "seconds")

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/木道寻08/article/detail/860719
推荐阅读
相关标签
  

闽ICP备14008679号