Python爬虫-第四章-1-多线程多进程并发爬取Ⅱ

时间:2023-01-15 21:57:51

线程池使用案例:

  • 一次性开辟一些线程,用户直接给线程池提交任务,线程任务的调度交给线程池
# Demo Describe:线程池和进程池

from concurrent.futures import ThreadPoolExecutor, ProcessPoolExecutor


# start--------1,线程池----------------------
def fn(name):
for i in range(1000):
print(name, i)


if __name__ == '__main__':
# 线程池里开启50个线程处理任务
with ThreadPoolExecutor(50) as t:
for i in range(100):
t.submit(fn, name=f'线程{i}')
print('处理完毕') # 线程池任务完毕才会继续执行-线程守护

# end--------1,线程池----------------------

# start--------2,进程池----------------------
# def fn(name):
# for i in range(1000):
# print(name,i)
#
# if __name__ == '__main__':
# # 线程池里开启50个线程处理任务
# with ProcessPoolExecutor(50) as t:
# for i in range(100):
# t.submit(fn,name=f'线程{i}')
# print('处理完毕') # 线程池任务完毕才会继续执行-线程守护

# end--------2,进程池----------------------