python3多进程基础实践

声明:参考B站视频,自学成长记录
https://www.bilibili.com/video/BV1zy4y1M7xt?p=9

多进程

'''
>多线程
    计算 类型的操作,用多进程
    IO 类型的操作,用多线程
    进程切换代价要高于线程切换
'''

计算密集型 - 多线程 与 多进程√ 对比

# 多线程 实现大量计算(消耗CPU)的操作
import time
from concurrent.futures import ThreadPoolExecutor, as_completed

def fib(n):
    if n <=2:
        return 1
    return fib(n-1)+fib(n-2)

with ThreadPoolExecutor(3) as executor:
    all_task = [executor.submit(fib,(num)) for num in range(30,35)]
    start_time = time.time()
    for future in as_completed(all_task):
        data = future.result()
        print('exe result:{}'.format(data))
    print('last time is:{}'.format(time.time()-start_time))

'''
运行结果:
    last time is:9.496543169021606
'''
# 多进程 实现大量计算(消耗CPU)的操作
import time
from concurrent.futures import ProcessPoolExecutor,as_completed

def fib(n):
    if n <=2:
        return 1
    return fib(n-1)+fib(n-2)

if __name__ == '__main__':
    # windows中不在main中会报:concurrent.futures.process.BrokenProcessPool
    with ProcessPoolExecutor(3) as executor:
        all_task = [executor.submit(fib,(num)) for num in range(30,35)]
        start_time = time.time()
        for future in as_completed(all_task):
            data = future.result()
            print('exe result:{}'.format(data))
        print('last time is:{}'.format(time.time()-start_time))
'''
运行结果:
    last time is:5.928837776184082
'''

IO密集型 - 多线程√ 与 多进程 对比

# 多线程 实现大量IO(消耗磁盘)的操作
import time
from concurrent.futures import ThreadPoolExecutor, as_completed

def time_sleep(n):
    time.sleep(n)
    return n


with ThreadPoolExecutor(3) as executor:
    all_task = [executor.submit(time_sleep,(num)) for num in [2]*5 ]
    start_time = time.time()
    for future in as_completed(all_task):
        data = future.result()
        print('exe result:{}'.format(data))
    print('last time is:{}'.format(time.time()-start_time))

'''
运行结果:
    last time is:4.00107741355896
'''
# 多进程 实现大量IO(消耗磁盘)的操作
import time
from concurrent.futures import ProcessPoolExecutor,as_completed

def time_sleep(n):
    time.sleep(n)
    return n

if __name__ == '__main__':
    # 不在main中会报:concurrent.futures.process.BrokenProcessPool
    with ProcessPoolExecutor(3) as executor:
        all_task = [executor.submit(time_sleep,(num)) for num in [2]*5]
        start_time = time.time()
        for future in as_completed(all_task):
            data = future.result()
            print('exe result:{}'.format(data))
        print('last time is:{}'.format(time.time()-start_time))
'''
运行结果:
    last time is:4.298689603805542
'''

版权声明:本文为BuLingLings原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。