栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

Python编程基础:第六十节 多进程Multiprocessing

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Python编程基础:第六十节 多进程Multiprocessing

第六十节 多进程Multiprocessing
  • 前言
  • 实践

前言

多进程能够在不同的CPU核心上并行运行任务,可以绕过用于线程的GIL。
多进程:更适合密集计算相关任务(cpu使用率高)。
多线程:更好地处理与IO相关的任务。

实践

我们来实现一个数据求和的功能,例如从0加到100000000。首先我们使用单个进程进行计算:

from multiprocessing import Process, cpu_count
import time
result = {}
def counter(num_1, num_2, name):
    count = 0
    for i in range(num_1, num_2):
        count += i
    result[name] = count
    print(result)

def main():
    a = Process(target=counter, args=(0, 100000000, '进程1'))
    a.start()
    a.join()
    print('运行耗时: {} 秒'.format(time.perf_counter()))

if __name__ == '__main__':
    main()
>>> {'进程1': 4999999950000000}
>>> 运行耗时: 13.5747503 秒

接下来我们使用两个进程进行计算:

from multiprocessing import Process, cpu_count
import time
result = {}
def counter(num_1, num_2, name):
    count = 0
    for i in range(num_1, num_2):
        count += i
    result[name] = count
    print(result)

def main():
    a = Process(target=counter, args=(0, 50000000, '进程1'))
    a.start()
    b = Process(target=counter, args=(50000000, 100000000, '进程2'))
    b.start()

    a.join()
    b.join()
    print('运行耗时: {} 秒'.format(time.perf_counter()))

if __name__ == '__main__':
    main()
>>> {'进程1': 1249999975000000}
>>> {'进程2': 3749999975000000}
>>> 运行耗时: 9.9812741 秒

接下来我们尝试跑满所有的CPU核心:

from multiprocessing import Process, cpu_count
import time
result = {}
def counter(num_1, num_2, name):
    count = 0
    for i in range(num_1, num_2):
        count += i
    result[name] = count
    print(result)

def main():
    print(cpu_count())  # 计算本机的CPU核心数目
    a = Process(target=counter, args=(0, 25000000, '进程1'))
    a.start()
    b = Process(target=counter, args=(25000000, 50000000, '进程2'))
    b.start()
    c = Process(target=counter, args=(50000000, 75000000, '进程3'))
    c.start()
    d = Process(target=counter, args=(75000000, 100000000, '进程4'))
    d.start()

    a.join()
    b.join()
    c.join()
    d.join()
    print('运行耗时: {} 秒'.format(time.perf_counter()))

if __name__ == '__main__':
    main()
>>> 4
>>> {'进程3': 1562499987500000}
>>> {'进程1': 312499987500000}
>>> {'进程2': 937499987500000}
>>> {'进程4': 2187499987500000}
>>> 运行耗时: 5.9548173 秒

发现当跑满所有的CPU核心时速度是最快的(并不是越多越好哦,再多的话速度反而慢了),所以我们一般设定多进程的进程数目为CPU的核心数目

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/529630.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号