学习笔记(33):Python网络编程&并发编程-进程池线程池
2023-09-11 14:21:45 时间
立即学习:https://edu.csdn.net/course/play/24458/296451?utm_source=blogtoedu
进程池与线程池:
一般应用在网站上,进程池或线程池最大的数量一般需要尽可能地大但是不要超出服务器的承载范围
1.进程池:
1)concurrent.futures.ProcessPoolExecutor(n)
2)创建了一个进程池,池中含有n个进程,任务都由这n个进程来完成
3)n默认为cpu的核数
4)pool.shutdown():表示关闭了新提交任务给进程池的入口,且等待进程池执行结束后再进行后面的代码
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor#进程池与线程池
import os,random,time
def processpool(name):
print('pid:%s ; %s:running'%(os.getpid(),name))
time.sleep(random.randint(2,5))
if __name__ == '__main__':
pool = ProcessPoolExecutor(5)#创建一个进程池,池子里面有5个进程
for i in range(10):
pool.submit(processpool,'任务%s'%i)
pool.shutdown(wait=True)
print('zhu')
运行结果:由结果可知,进程的pid只有5个,10个任务一直都是由这5个进程来完成
'''
"F:\software install\python3.6.4\python.exe" C:/Users/jinlin/Desktop/python_further_study/并发编程/进程池线程池.py
pid:11848 ; 任务0:running
pid:13740 ; 任务1:running
pid:12848 ; 任务2:running
pid:7196 ; 任务3:running
pid:10884 ; 任务4:running
pid:11848 ; 任务5:running
pid:13740 ; 任务6:running
pid:12848 ; 任务7:running
pid:7196 ; 任务8:running
pid:10884 ; 任务9:running
zhu
进程已结束,退出代码0
'''
2.线程池
1)concurrent.futures.ThreadPoolExecutor(n)
2)创建了一个线程池,池中含有n个线程,任务都由这n个线程来完成
3)pool.shutdown():表示关闭了新提交任务给线程池的入口,且等待线程池执行结束后再进行后面的代码
from concurrent.futures import ProcessPoolExecutor,ThreadPoolExecutor#进程池与线程池
from threading import currentThread
import os,random,time
def processpool(name):
print('%s pid:%s ; %s:running'%(currentThread().getName(),os.getpid(),name))
time.sleep(random.randint(2,5))
if __name__ == '__main__':
pool = ThreadPoolExecutor(5)#创建一个进程池,池子里面有5个进程
for i in range(10):
pool.submit(processpool,'任务%s'%i)
pool.shutdown()
print('zhu')
运行结果:由结果可以知道,线程池的所有线程的pid都是一样的,那是因为线程池中的线程都是在同一个进程中,因此pid会一致,且只有5个线程在执行任务
'''
"F:\software install\python3.6.4\python.exe" C:/Users/jinlin/Desktop/python_further_study/并发编程/进程池线程池.py
ThreadPoolExecutor-0_0 pid:11888 ; 任务0:running
ThreadPoolExecutor-0_1 pid:11888 ; 任务1:running
ThreadPoolExecutor-0_2 pid:11888 ; 任务2:running
ThreadPoolExecutor-0_3 pid:11888 ; 任务3:running
ThreadPoolExecutor-0_4 pid:11888 ; 任务4:running
ThreadPoolExecutor-0_3 pid:11888 ; 任务5:running
ThreadPoolExecutor-0_2 pid:11888 ; 任务6:running
ThreadPoolExecutor-0_1 pid:11888 ; 任务7:running
ThreadPoolExecutor-0_4 pid:11888 ; 任务8:running
ThreadPoolExecutor-0_0 pid:11888 ; 任务9:running
zhu
进程已结束,退出代码0
'''
相关文章
- 安装python环境(win7 64bit)
- python工具——basemap使用二绘制中国地图
- 【Python五篇慢慢弹(3)】函数修行知python
- Python 日期和时间_python 当前日期时间_python日期格式化
- python:pip升级pip本身和setuptools(Python 3.7.15)
- 华为OD机试 - 创建二叉树(Java & JS & Python)
- 华为OD机试 - 最佳对手(Java & JS & Python)
- 华为OD机试 - 识图谱新词挖掘(Java & JS & Python)
- 华为OD机试 - 静态扫描(Java & JS & Python)
- 华为OD机试 - 查找重复代码(Java & JS & Python)
- 华为OD机试 - 没有回文串(Java & JS & Python)
- 华为OD机试 - 等和子数组最小和(Java & JS & Python)
- 华为OD机试 - 计算疫情扩散时间(Java & JS & Python)
- 我00后,会Python,月薪5000,兼职1.5w
- Python语言编程学习:利用python输出当前python版本、MSC版本型号
- Python:利用python编程实现三维图像绘制展示(六面体旋转、三维球柱状体、下雪场景等)
- 〖Python零基础入门篇⑨〗 - 数据类型介绍
- 〖Python WEB 自动化测试实战篇③〗- python-selenium环境配置搭建
- “华数杯”建模学习思考(Matlab&Python代码实现)
- 【华为OD机试Python实现】HJ103 Redraiment的走法(中等)
- 【华为机试真题 Python实现】最优策略组合下的总的系统消耗资源数
- python并发编程&多进程(二)
- python如何去掉字符串‘xa0’
- Python 小白从零开始 PyQt5 项目实战(8)汇总篇(完整例程)
- 【异常】前端ERR! stack Error: Can‘t find Python executable “python“, you can set the PYTHON env variable.
- python工具方法 21 应用于语义分割、图像分类、自动编码机的图像随机mask方法(Cutout方法、Random Erasing方法)
- 【Python实战】 ---- python 自带的 venv 虚拟环境更新 pip 失败