【python】多进程小结

every blog every motto: Light tomorrow with today.

0. 前言

本节对python中的多进程进行实验和小结。
建议: 先看多线程的概念,本节不再重复概念,点我

1. 正文

1.1 装饰器

装饰器,用于查看函数运行时间,点我点我

import threading
import time
import functools
from multiprocessing import Process

c = 0


def count_time(func):
    """装饰器:计算程序运行时间"""

    @functools.wraps(func)
    def wrapper(*args, **kwargs):
        t1 = time.time()
        res = func(*args, **kwargs)
        t2 = time.time()

        # 计算时间
        time_consumed = t2 - t1
        print('{}函数一共花费了{}秒'.format(func.__name__, time_consumed))
        return res

    return wrapper

1.2 多进程测试部分

1.2.1 被测试函数为两个函数

被测试函数如下:

@count_time
def mop_floor(arg):
    """拖地操作,"""
    global c
    print('开始拖地……')
    for i in range(3):
        time.sleep(1)
        c += 5
        c -= 5
        print('{} is running……c={} !!'.format(arg, c))
    print('---------地拖完了!-----------')


@count_time
def heat_water(arg):
    """烧水操作"""
    global c
    print('我要烧水了……')
    for i in range(4):
        time.sleep(1)
        print('{} is running……c={} !!'.format(arg, c))
    print('------------水烧开了!-------------')

1>. 单进程

单进程函数:

@count_time
def single_processing():
    """单进程"""
    # 对两个函数进行测试
    mop_floor('1')
    heat_water('2')
    print('-' * 100)

主函数:

def main():
    # 单进程
    single_processing()

if __name__ == '__main__':
    main()

单进程运行结果:
在这里插入图片描述

2>. 多进程

多进程函数

@count_time
def my_processing():
    p1 = Process(target=mop_floor, args=('1',))
    p2 = Process(target=heat_water, args=('2',))
    p1.start()
    p2.start()
    p1.join()
    p2.join()

    # -----------------------------------------
    # process_array = {}
    # n = 1
    # for tid in range(n):
    #     t = Process(target=single_func_tested, args=('1',))
    #     t.start()
    #     process_array[tid] = t
    # for i in range(n):
    #     process_array[i].join()

    print('-' * 100)

主函数

def main():

    # 多进程
    my_processing()


if __name__ == '__main__':
    main()

结果,可以发现,总时间减少了
在这里插入图片描述

1.2.2 被测试函数为一个计算型函数

被测试函数

@count_time
def single_func_tested(arg):
    """对一个函数进行测试"""
    sum = 0
    for i in range(10000000):  # 100000
        sum += i

    print('{} over'.format(arg))

1>. 单进程

单进程函数

@count_time
def single_processing():
    """单进程"""

    # 对一个函数进行测试
    single_func_tested('1')
    print('-' * 100)

主函数:

def main():
    # 单进程
    single_processing()

    # 多进程
    # my_processing()


if __name__ == '__main__':
    main()

单进程计算结果:
在这里插入图片描述

2>. 多进程

多进程函数

@count_time
def my_processing():

    process_array = {
    
    }
    n = 1
    for tid in range(n):
        t = Process(target=single_func_tested, args=('1',))
        t.start()
        process_array[tid] = t
    for i in range(n):
        process_array[i].join()

    print('-' * 100)

主函数:

def main():


    # 多进程
    my_processing()


if __name__ == '__main__':
    main()

当n=1,
在这里插入图片描述
当n=2,
在这里插入图片描述
当n=3,
在这里插入图片描述
当n=4,
在这里插入图片描述
当n=5,
在这里插入图片描述
小结:

  1. 当n<=4时,程序总运行时间不变,这是因为实验机器为4核,所以开4个以内进程时,核够用,当n> 4时,程序运行时间有所增加,即所开进程超过核心数。
  2. 开进程的条件时多个CPU,或者1个CPU有多个核心
  3. 对单个程序开多进程,并不会缩短程序运行时间,仅将单个程序多运行几次而已

1.2.3 被测试函数为一个等待型函数

被测试函数

@count_time
def mop_floor(arg):
    """拖地操作,"""
    global c
    print('开始拖地……')
    for i in range(3):
        time.sleep(1)
        c += 5
        c -= 5
        print('{} is running……c={} !!'.format(arg, c))
    print('---------地拖完了!-----------')

1>. 单进程

单进程函数

@count_time
def single_processing():
    """单进程"""
    # 对两个函数进行测试
    mop_floor('1')
    # heat_water('2')

    # 对一个函数进行测试
    # single_func_tested('1')
    print('-' * 100)

主函数

def main():
    # 单进程
    single_processing()



if __name__ == '__main__':
    main()

结果:
在这里插入图片描述

2>. 多进程

多进程函数:

@count_time
def my_processing():

    n = 1
    for tid in range(n):
        t = Process(target=mop_floor, args=('1',))
        t.start()
        process_array[tid] = t
    for i in range(n):
        process_array[i].join()

    print('-' * 100)

主函数:

def main():


    # 多进程
    my_processing()


if __name__ == '__main__':
    main()

当n=1,
在这里插入图片描述
当n=2,
在这里插入图片描述
当n=3,
在这里插入图片描述
当n=4,
在这里插入图片描述
当n=5,
在这里插入图片描述
当n=8,
在这里插入图片描述
小结:

  1. 当进程说超过核心说时,总运行时间有些许增加,增加程度不如计算型函数明显。
  2. 对单个程序开多进程,并不会缩短程序运行时间,仅将单个程序多运行几次而已

以上。


总结:

  1. 开多进程的条件是,多个CPU或是1个多核CPU
  2. 对单个程序开多进程,无作用

1.3 源码

https://gist.github.com/onceone/f5489dcc1499c81ee0161f0ea3bb442b

参考文献

[1] https://blog.csdn.net/weixin_39190382/article/details/107107980
[2] http://www.uml.org.cn/python/201901221.asp
[3] https://www.jianshu.com/p/644dbb6d4cc8
[4] https://blog.csdn.net/m0_37324740/article/details/85765167
[5] https://www.cnblogs.com/-qing-/p/11291581.html
[6] https://blog.csdn.net/lzy98/article/details/88819425
[7] https://www.cnblogs.com/yssjun/p/11302500.html
[8] https://www.jb51.net/article/167165.htm
[9] https://blog.csdn.net/weixin_44850984/article/details/89165731
[10] https://www.cnblogs.com/justbreaking/p/7218909.html?utm_source=itdadao&utm_medium=referral

猜你喜欢

转载自blog.csdn.net/weixin_39190382/article/details/107207900
今日推荐