python 的进程池和线程池
Python 2024/6/1 16:52:14 点击:不统计
www-fo-a-sp.cn
python线程池,进程池案例代码
import time
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import ProcessPoolExecutor
def run(v, v2):
time.sleep(2)
return str(v) + "a" + str(v2)
pool_objs = []
# 创建线程池
# pool = ThreadPoolExecutor(max_workers=5)
# 创建 进程池
pool = ProcessPoolExecutor(max_workers=5)
for i in range(10):
pool_obj = pool.submit(run, i, i + 1)
pool_objs.append(pool_obj)
for i, future in enumerate(pool_objs):
result = future.result() # 这会阻塞直到每个任务完成
print(i, result)
原文章%77w%77%2Ef%6F%72%61%73%70%2E%63n
·上一篇:python async await >> ·下一篇:python redis 连接池 >>