python 线程池 concurrent.futures ThreadPoolExecutor

python 线程池 concurrent.futures ThreadPoolExecutor

步骤:
1,导包from concurrent.futures import ThreadPoolExecutor
2,定义功能模块
3,开启线程池
4,将功能模块加入到线程池中运行

应用:一般是同一个功能模块,放到线程池中运行,运行完成后自动归还到线程池中

from concurrent.futures import ThreadPoolExecutor
#线程池的使用,主要用于同一功能,放到线程池中处理,python设置上限,程序放到线程池中处理,完成后,线程自动归还到线程池中
#创建1个功能模块
def fun(name):
    for i in range(100):
        print("子线程",name,i)
        
#主线程
if __name__ == '__main__':
    with ThreadPoolExecutor(20) as t:   #定义1个20的线程池
        list = ["dog","cat","fish","apple"]
        for i in list:
            t.submit(fun,f"{i}")
#         t.submit(fun,"dog")
#         t.submit(fun,"cat")
#         t.submit(fun,"fish")
#         t.submit(fun,"apple")

你可能感兴趣的:(python,python)