python进程池的使用注意
python进程池的使用注意
1、使用注意
(1)使用池时,如果未指定进程数,则默认为CPU核心数。
(2)核心数量对应于计算机的逻辑处理器数量(任务管理器-性能),而不是核心数量(我的计算机有2个核心和4个逻辑处理器,因此这里默认使用4个进程)
(3)进程数可以是几十万,并不意味着开放进程的数量是4。只要使用池(10),就可以同时打开10个进程进行爬网。
(4)但是需要注意的是,无论多线程还是多进程,打开过多都会导致切换耗时,降低效率,所以创建过多的多线程和进程是谨慎的。
2、实例
importrequests
frombs4importBeautifulSoup
frommultiprocessingimportPool,current_process
defget_title(i):
print('start',current_process().name)
title_list=[]
url='https://movie.douban.com/top250?start={}&filter='.format(i*25)
r=requests.get(url)
soup=BeautifulSoup(r.content,'html.parser')
lis=soup.find('ol',class_='grid_view').find_all('li')
forliinlis:
title=li.find('span',class_="title").text
#returntitle
title_list.append(title)
print(title)
return(title_list)if__name__=='__main__':
pool=Pool()
foriinrange(10):
pool.apply_async(get_title,(i,))
pool.close()
pool.join()
print('finish')
以上就是python进程池的使用注意,希望能对大家有所帮助。更多Python学习教程请关注IT培训机构:千锋教育。

猜你喜欢LIKE
相关推荐HOT
更多>>
Python如何在画布中绘制多个图表?
python如何在画布中绘制多个图表?1、说明在绘制图表时,Matplotlib模块默认先创建一块画布,然后将绘制的图表显示在画布上。若要在一个画布上画...详情>>
2023-11-14 21:04:27
python@是什么意思
python中@是什么意思通常我们所见的@符号是用于邮箱中,而在python中@符号也有着重要的作用,一个是表示修饰符,另一个则是表示矩阵乘法。表示...详情>>
2023-11-14 20:07:01
python进程池的使用注意
python进程池的使用注意1、使用注意(1)使用池时,如果未指定进程数,则默认为CPU核心数。(2)核心数量对应于计算机的逻辑处理器数量(任务管理器-...详情>>
2023-11-14 18:42:08
python多线程中join的应用场景
python多线程中join的应用场景1、线程的join()方法表示等这个线程运行完毕,程序再往下运行。我们来看下面的例子fromthreadingimportThreadt=ti...详情>>
2023-11-14 18:16:56热门推荐
python中scipy.linalg.inv()功能是什么?
沸requests在python中如何发送请求
热python自定义模块的注意点
热python如何将相对路径转换为绝对路径?
新python在列表中和列表末尾添加元素的方法
Python如何在画布中绘制多个图表?
pythonQueue在python的实例化
python@是什么意思
pythonconftest如何使用?
python列表运算详解
python进程池的使用注意
python多线程中join的应用场景
python异步中loop抛出异常的解决
pythontime模块处理系统时间的函数
技术干货






