1.线程池的原理

image.png

2.使用线程池的好处

image.png

3.ThreadPoolExecutor的使用用法

image.png

4.使用线程池改造爬虫程序

  1. def do_task(n):
  2. if n > 5:
  3. print(n, '开始做任务')
  4. time.sleep(0.5)
  5. res = str(n) + '你真漂亮'
  6. return res
  7. else:
  8. print('没做任务')
  9. tasks = [7, 9, 10, 12]
  10. import concurrent.futures
  11. with concurrent.futures.ThreadPoolExecutor() as pool:
  12. strs = list(pool.map(do_task, tasks))
  13. for str2 in strs:
  14. print('map', str2)
  15. print('map over')
  16. with concurrent.futures.ThreadPoolExecutor() as pool:
  17. strs1 = [pool.submit(do_task, task) for task in tasks]
  18. for task in concurrent.futures.as_completed(strs1):
  19. print(task.result())
  20. print(strs)
  21. print('submit over')