1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
from concurrent.futures import ThreadPoolExecutor
from queue import Queue
#导包

class BoundedThreadPoolExecutor(ThreadPoolExecutor):
#重写线程池类,使创建的任务数量为最大线程的2倍
def __init__(self, max_workers=None, thread_name_prefix=''):
super().__init__(max_workers, thread_name_prefix)
self._work_queue = Queue(max_workers * 2)

executor = BoundedThreadPoolExecutor(max_workers=8)#创建实例

hrefs=['www.1.com','www.2.com','www.2.com']
for href in hrefs:
executor.submit(parser_img, href)
#submit创建单个线程

executor.submit(parser_img, hrefs)
#map创建多个线程