python基础——池

池的介绍:

提前创建进程池,防止创建的进程数量过多导致系统性能受到影响,在系统执行任务时,系统会使用池中已经创建进程/线程,从而防止资源的浪费,创建的进程/线程可以让多个进程使用,从而降低了操作系统的负担,加快了系统执行的效率;

若果不创建进程池,可能会导致任务建立的进程数量过多,影响系统,并且在执行不同任务时,还会重新进行进程的创建,导致资源的浪费

池的使用:

进程池:

ProcessPoolExecutor

进程池的使用:
python 复制代码
from threading import current_thread
from concurrent.futures import ThreadPoolExecutor,ProcessPoolExecutor
def func(i):
    print("进程号:{}\n".format(current_thread().ident))
    print("进程{}\n".format(i))

tp = ThreadPoolExecutor(4)
ts = ProcessPoolExecutor(10)
if __name__ == '__main__':
    for i in range(100):
        tp.submit(func,i)

运行结果:

由于此时池中创建了四个进程,因此函数func()在执行多次的过程中会循环的使用四个已经创建的进程(如上图)

不使用进程池:
python 复制代码
from threading import current_thread
from multiprocessing import Process

def func(i):
    print("进程号:{}".format(current_thread().ident))
    print("进程{}".format(i))

if __name__ == '__main__':
    for i in range(10):
        t = Process(target=func,args=(i,)).start()

运行结果:

两者对比可以看到,由于后者未使用进程池,因此,在func()函数执行了10次时,系统创建了十个进程,而前者使用了建立了四个进程的进程池,因此,即使func()函数执行了100次,系统也只创建了4个进程

线程池:

不使用线程池:

python 复制代码
from threading import current_thread
from threading import Thread

def func(i):
    print("线程号:{}".format(current_thread().ident))
if __name__ == '__main__':
    for i in range(10):
        t = Thread(target=func,args=(i,)).start()

运行结果:

有结果可以看到:使用线程运行了十次func()函数,系统创建了十个进程用来执行此任务

使用线程池:

python 复制代码
from threading import current_thread
from concurrent.futures import ThreadPoolExecutor

def func(i):
    print("线程号:{}".format(current_thread().ident))
if __name__ == '__main__':
    tp = ThreadPoolExecutor(20)
    for i in range(100):
        tp.submit(func,i)

运行结果:

执行100次,但是被使用的线程仅有创建的20个,节省了系统的资源

使用map()方法进行函数的传递

python 复制代码
from threading import current_thread
from concurrent.futures import ThreadPoolExecutor

def func(i):
    print("线程号:{}".format(current_thread().ident))
    print(i)
if __name__ == '__main__':
    tp = ThreadPoolExecutor(4)
    ret = tp.map(func,(i for i in range(10)))
    print(ret)

回调函数:

python 复制代码
from threading import current_thread
from concurrent.futures import ThreadPoolExecutor

def func(i):
    print("线程号:{}".format(current_thread().ident))
    print(i)
    return  i**2
def print_func(ret,i):
    print("我是print_func()函数")
    print(ret.result())

if __name__ == '__main__':
    tp = ThreadPoolExecutor(4)
    for i in range(20):
        ret = tp.submit(func,i)
        ret.add_done_callback(print_func)

运行结果:

使用回调函数可以保证ret对象在执行完毕之后立即执行回调函数的参数对象

进程创建数量的建议:

进程数量:建议进程数量为cpu数量的一倍 到cpu数量的二倍之间

线程数量:建议线程数量为cpu数量的五倍

相关推荐
云飞云共享云桌面3 小时前
医疗器械设备研发:多人同时操作 SolidWorks,怎样依靠单台服务器替代多工作站
运维·服务器·网络·数据库·制造
T型码农要学习3 小时前
开源项目5|FileBrowser:免费在线文件管理器!随时随地管控服务器文件
运维·服务器·人工智能·开源
朱 欢 庆4 小时前
云服务器附件备份到本机内网服务器
运维·服务器·前端·经验分享
Sylvia33.4 小时前
LOL实时数据接入深度解析:从WebSocket到完整数据模型
java·网络·python·websocket·网络协议
梦想不只是梦与想4 小时前
Web 服务器网关协议:ASGI 和 WSGI
服务器·python·fastapi
智恒百亿4 小时前
8 卡 RTX 5090 服务器深度实测:256GB 显存能否支撑 70B 模型微调?选型参考
大数据·运维·服务器·人工智能
ai小陈6 小时前
PyTorch DataLoader数据加载性能排查:GPU利用率低的实操指南
人工智能·pytorch·python·深度学习·ai·gpu算力
2601_962299249 小时前
Azure python操作系统列表
python·操作系统·azure·虚拟机·存储配置文件
小诗懂技术9 小时前
【网络通信UDP】基础 进程间不同主机的通信
运维·服务器·网络
weixin_440730509 小时前
selenium实战记录-从登陆-首页-进入应用流程用例三种参数化+日志+报告
服务器·selenium·测试工具