Skip to content

下载图片 in thread-pool (ThreadPoolExecutor)

python
from concurrent.futures import ThreadPoolExecutor, as_completed
import os
import requests
import time
import traceback

dir_d = 'd'
if not os.path.exists(dir_d):
    os.mkdir(dir_d)

def save_jpg(index, content):
    with open(os.path.join(dir_d, f'{index}.jpg'), 'wb') as fp:
        fp.write(content)


def work(task):
    try:
        index = task.get('index')
        url = task.get('url')
        content = requests.get(url=url, timeout=3).content
        save_jpg(index, content)
        print('finished', index)
    except Exception as e:
        pass
    return index

l = ['http://wx2.sinaimg.cn/mw600/0089jzyPly1gqo2l5gwb7j31400u04qp.jpg']*2000

def main():
    t = time.monotonic()
    with ThreadPoolExecutor(100) as e:
        # fs = []
        for index,url in enumerate(l):
            task = dict(url=url,index=index)
            f = e.submit(work, task)
        #     fs.append(f)
        # for f in as_completed(fs):
        #     print(f.result())

    print('task done', time.monotonic()-t)

if __name__ == '__main__':
    main()

Released under the MIT License.