Thread pool, for example, we have a method to run multiple threads 1000, we can not declare 1000 threads, this time we can set up 50 threads through the thread pool, when it will run automatically assigned to those of 1000, that is, each thread 20 times
The following example is the thread pool examples:
Import Requests, Threading Import faker Import ThreadPool # Thread Pool module F = faker.Faker (the locale = " ZH-the CN " ) DEF down_load_file (URL): R & lt = requests.get (URL) # GET request m = f.name () # randomly generated file name with Open ( ' ./pic/%s.jpg ' % m, ' WB ' ) aS FW: # image writing binary file, save as jpg format fw.write (r.content) URL_LIST = [ 'http://www.nnzhp.cn/wp-content/uploads/2019/02/jiami.jpeg ' , ' http://www.nnzhp.cn/wp-content/uploads/2019/03/js.png ' , ' http://www.nnzhp.cn/wp-content/uploads/2018/08/ab389f04cb5b57344ef9655428bccaec.png ' ] the pool = threadpool.ThreadPool (3) # create a thread pool thread pool specify a maximum number of threads reqs threadpool.makeRequests = (down_load_file, URL_LIST) # call to start the thread used to generate parameters [pool.putRequest (REQ) for REQ in reqs] # cycle start thread Print ( " the current number of threads ", threading.activeCount ()) # The current number of threads number of threads, including the main thread of statistics pool.wait () # wait for the child threads Print ( " Test End " )