PythonStudy——进程池与线程池
获取当前运行环境的CPU核心数(线程数)
import os,time
# 获取当前运行环境的CPU核心数(线程数)
print(os.cpu_count())
# 8
进程池(线程池)涉及到的专业英文词汇
进程池(线程池)特点
线程池,不仅帮我们管理了线程的开启和销毁,还帮我们管理任务的分配
特点: 线程池中的线程只要开启之后 即使任务结束也不会立即结束 因为后续可能会有新任务
避免了频繁开启和销毁线程造成的资源浪费
进程池(线程池)生成器的导包语句
# 线程池生成器
from concurrent.futures import ThreadPoolExecutor
# 进程池生成器
from concurrent.futures import ProcessPoolExecutor
创建进程池(线程池)实例:创建池子
# 创建池子实例 可以指定池子里面有多少线程
# 如果不指定则默认设置为计算机(核心线程数*5) 我的CPU为4核8线程 建议设置为40
# 线程池最大值,应该机器所能承受的最大值,当然需要考虑你的机器有几个任务要做
pool = ThreadPoolExecutor(40)
线程池的使用
pool = ThreadPoolExecutor(40)
from threading import enumerate,current_thread
import time
def task(name,age):
print("Name: %s Age: %s"%(name,age))
time.sleep(2)
# 把提交任务到线程池中,创建线程任务(task)
# 任务的参数,直接写到后面不需要定义参数名称,因为是可变位置参数
# 函数名 函数参数
# 提交方式 一 | |
task1 = pool.submit(task,"Venti",18)
# 提交方式 二
pool.submit(task,"Jack",20)
print(enumerate())
# <_MainThread(MainThread, started 140735554691968)>,
# <Thread(ThreadPoolExecutor-0_0, started daemon 123145333833728)>,
# <Thread(ThreadPoolExecutor-0_1, started daemon 123145339088896)>]
进程池的使用
# 进程池的使用 同样可以设置最大进程数量 默认为cpu的个数
# 具体的值也要参考机器性能
pool = ProcessPoolExecutor()
def task(name):
print(os.getpid())
print(name)
if __name__ == '__main__':
pool.submit(task,"venti")
pool.shutdown()
pool.submit(task, "tom")
进程池(线程池)的shutdown
等待所有任务(主线程,子线程)结束 销毁掉所有线程 释放线程池对象
from concurrent.futures import ThreadPoolExecutor
from threading import current_thread, enumerate
import time
pool = ThreadPoolExecutor(50)
def task():
# name 没有 getName 信息详细
print(current_thread().name)
print(current_thread().getName())
time.sleep(0)
# 循环创建50个子线程
for i in range(50):
pool.submit(task)
start_time = time.time()
print(enumerate())
# 等待所有任务结束 销毁掉所有线程 释放线程池
pool.shutdown()
end_time = time.time()
print("Used time: %s " % (end_time - start_time))
# ThreadPoolExecutor-0_0
# ThreadPoolExecutor-0_0
# ThreadPoolExecutor-0_0
# ThreadPoolExecutor-0_0
# ThreadPoolExecutor-0_1
# ThreadPoolExecutor-0_1
# ThreadPoolExecutor-0_1
# ThreadPoolExecutor-0_1
# ThreadPoolExecutor-0_4
# ThreadPoolExecutor-0_4
# ThreadPoolExecutor-0_2
# ThreadPoolExecutor-0_4
# ThreadPoolExecutor-0_4
# ThreadPoolExecutor-0_2
# ThreadPoolExecutor-0_1
# ThreadPoolExecutor-0_1
# ThreadPoolExecutor-0_5
# ThreadPoolExecutor-0_5
# ThreadPoolExecutor-0_5
# ThreadPoolExecutor-0_5
# ThreadPoolExecutor-0_2
# ThreadPoolExecutor-0_2
# ThreadPoolExecutor-0_0
# ThreadPoolExecutor-0_0
# ThreadPoolExecutor-0_1
# ThreadPoolExecutor-0_1
# ThreadPoolExecutor-0_3
# ThreadPoolExecutor-0_3
# ThreadPoolExecutor-0_9
# ThreadPoolExecutor-0_9
# ThreadPoolExecutor-0_2
# ThreadPoolExecutor-0_2
# ThreadPoolExecutor-0_10
# ThreadPoolExecutor-0_10
# ThreadPoolExecutor-0_7
# ThreadPoolExecutor-0_7
# ThreadPoolExecutor-0_12
# ThreadPoolExecutor-0_12
# ThreadPoolExecutor-0_1
# ThreadPoolExecutor-0_1
# ThreadPoolExecutor-0_5
# ThreadPoolExecutor-0_5
# ThreadPoolExecutor-0_9
# ThreadPoolExecutor-0_9
# ThreadPoolExecutor-0_14
# ThreadPoolExecutor-0_14
# ThreadPoolExecutor-0_14
# ThreadPoolExecutor-0_14
# ThreadPoolExecutor-0_15
# ThreadPoolExecutor-0_15
# ThreadPoolExecutor-0_15
# ThreadPoolExecutor-0_15
# ThreadPoolExecutor-0_10
# ThreadPoolExecutor-0_10
# ThreadPoolExecutor-0_7
# ThreadPoolExecutor-0_7
# ThreadPoolExecutor-0_17
# ThreadPoolExecutor-0_17
# ThreadPoolExecutor-0_12
# ThreadPoolExecutor-0_12
# ThreadPoolExecutor-0_18
# ThreadPoolExecutor-0_18
# ThreadPoolExecutor-0_3
# ThreadPoolExecutor-0_3
# ThreadPoolExecutor-0_5
# ThreadPoolExecutor-0_5
# ThreadPoolExecutor-0_20
# ThreadPoolExecutor-0_20
# ThreadPoolExecutor-0_4
# ThreadPoolExecutor-0_4
# ThreadPoolExecutor-0_22
# ThreadPoolExecutor-0_22
# ThreadPoolExecutor-0_22
# ThreadPoolExecutor-0_22
# ThreadPoolExecutor-0_23
# ThreadPoolExecutor-0_23
# ThreadPoolExecutor-0_24
# ThreadPoolExecutor-0_24
# ThreadPoolExecutor-0_15
# ThreadPoolExecutor-0_15
# ThreadPoolExecutor-0_11
# ThreadPoolExecutor-0_11
# ThreadPoolExecutor-0_10
# ThreadPoolExecutor-0_10
# ThreadPoolExecutor-0_7
# ThreadPoolExecutor-0_7
# ThreadPoolExecutor-0_7
# ThreadPoolExecutor-0_7
# ThreadPoolExecutor-0_7
# ThreadPoolExecutor-0_7
# ThreadPoolExecutor-0_8
# ThreadPoolExecutor-0_8
# ThreadPoolExecutor-0_8
# ThreadPoolExecutor-0_8
# ThreadPoolExecutor-0_18
# ThreadPoolExecutor-0_18
# ThreadPoolExecutor-0_30
# ThreadPoolExecutor-0_30
# ThreadPoolExecutor-0_19
# ThreadPoolExecutor-0_19
# [<_MainThread(MainThread, started 140735554691968)>,
# <Thread(ThreadPoolExecutor-0_0, started daemon 123145317638144)>,
# <Thread(ThreadPoolExecutor-0_1, started daemon 123145322893312)>,
# <Thread(ThreadPoolExecutor-0_2, started daemon 123145328148480)>,
# <Thread(ThreadPoolExecutor-0_3, started daemon 123145333403648)>,
# <Thread(ThreadPoolExecutor-0_4, started daemon 123145338658816)>,
# <Thread(ThreadPoolExecutor-0_5, started daemon 123145343913984)>,
# <Thread(ThreadPoolExecutor-0_6, started daemon 123145349169152)>,
# <Thread(ThreadPoolExecutor-0_7, started daemon 123145354424320)>,
# <Thread(ThreadPoolExecutor-0_8, started daemon 123145359679488)>,
# <Thread(ThreadPoolExecutor-0_9, started daemon 123145364934656)>,
# <Thread(ThreadPoolExecutor-0_10, started daemon 123145370189824)>,
# <Thread(ThreadPoolExecutor-0_11, started daemon 123145375444992)>,
# <Thread(ThreadPoolExecutor-0_12, started daemon 123145380700160)>,
# <Thread(ThreadPoolExecutor-0_13, started daemon 123145385955328)>,
# <Thread(ThreadPoolExecutor-0_14, started daemon 123145391210496)>,
# <Thread(ThreadPoolExecutor-0_15, started daemon 123145396465664)>,
# <Thread(ThreadPoolExecutor-0_16, started daemon 123145401720832)>,
# <Thread(ThreadPoolExecutor-0_17, started daemon 123145406976000)>,
# <Thread(ThreadPoolExecutor-0_18, started daemon 123145412231168)>,
# <Thread(ThreadPoolExecutor-0_19, started daemon 123145417486336)>,
# <Thread(ThreadPoolExecutor-0_20, started daemon 123145422741504)>,
# <Thread(ThreadPoolExecutor-0_21, started daemon 123145427996672)>,
# <Thread(ThreadPoolExecutor-0_22, started daemon 123145433251840)>,
# <Thread(ThreadPoolExecutor-0_23, started daemon 123145438507008)>,
# <Thread(ThreadPoolExecutor-0_24, started daemon 123145443762176)>,
# <Thread(ThreadPoolExecutor-0_25, started daemon 123145449017344)>,
# <Thread(ThreadPoolExecutor-0_26, started daemon 123145454272512)>,
# <Thread(ThreadPoolExecutor-0_27, started daemon 123145459527680)>,
# <Thread(ThreadPoolExecutor-0_28, started daemon 123145464782848)>,
# <Thread(ThreadPoolExecutor-0_29, started daemon 123145470038016)>,
# <Thread(ThreadPoolExecutor-0_30, started daemon 123145475293184)>,
# <Thread(ThreadPoolExecutor-0_31, started daemon 123145480548352)>,
# <Thread(ThreadPoolExecutor-0_32, started daemon 123145485803520)>,
# <Thread(ThreadPoolExecutor-0_33, started daemon 123145491058688)>,
# <Thread(ThreadPoolExecutor-0_34, started daemon 123145496313856)>,
# <Thread(ThreadPoolExecutor-0_35, started daemon 123145501569024)>,
# <Thread(ThreadPoolExecutor-0_36, started daemon 123145506824192)>,
# <Thread(ThreadPoolExecutor-0_37, started daemon 123145512079360)>,
# <Thread(ThreadPoolExecutor-0_38, started daemon 123145517334528)>,
# <Thread(ThreadPoolExecutor-0_39, started daemon 123145522589696)>,
# <Thread(ThreadPoolExecutor-0_40, started daemon 123145527844864)>,
# <Thread(ThreadPoolExecutor-0_41, started daemon 123145533100032)>,
# <Thread(ThreadPoolExecutor-0_42, started daemon 123145538355200)>,
# <Thread(ThreadPoolExecutor-0_43, started daemon 123145543610368)>,
# <Thread(ThreadPoolExecutor-0_44, started daemon 123145548865536)>,
# <Thread(ThreadPoolExecutor-0_45, started daemon 123145554120704)>,
# <Thread(ThreadPoolExecutor-0_46, started daemon 123145559375872)>,
# <Thread(ThreadPoolExecutor-0_47, started daemon 123145564631040)>,
# <Thread(ThreadPoolExecutor-0_48, started daemon 123145569886208)>,
# <Thread(ThreadPoolExecutor-0_49, started daemon 123145575141376)>]
# Used time: 0.0021376609802246094