FreezeJ' Blog

python 多线程代码

2023-06-12

使用线程池

import concurrent.futures
import urllib.request

URLS = [
  'http://www.baidu.com/',
  'http://www.qq.com/',
  'http://durongjie.com/'
]

def load_url(url, timeout):
    with urllib.request.urlopen(url, timeout=timeout) as conn:
        return conn.read()

with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
    # executor.shutdown(wait=True)  # 等待所有任务完成

    # 每个任务执行后调用函数,阻塞的是子任务,不是阻塞等待所有完成。
    for future in concurrent.futures.as_completed(future_to_url):
        url = future_to_url[future]
        try:
            data = future.result()
        except Exception as exc:
            print('%s 产生异常 %s' % (url, exc))
        else:
            print('%s 页面大小为 %d bytes' % (url, len(data)))
Tags: Python