import concurrent.futures
import urllib.request
URLS = ['
http://www.foxnews.com/',
'
http://www.cnn.com/',
'
http://europe.wsj.com/',
'
http://www.bbc.co.uk/',
'
http://nonexistant-subdomain.python.org/']# Retrieve a single page and report the URL and contents
def load_url(url, timeout):
with urllib.request.urlopen(url, timeout=timeout) as conn:
return conn.read()
# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
# Start the load operations and mark each future with its URL
future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
for future in concurrent.futures.as_completed(future_to_url):
url = future_to_url[future]
try:
data = future.result()
except Exception as exc:
print('%r generated an exception: %s' % (url, exc))
else:
print('%r page is %d bytes' % (url, len(data)))
【 在 illers 的大作中提到: 】
: 假设需要做100个post请求,url和参数放在一个列表中。最后返回这100和请求的结果。
: 我现在是顺序执行,for循环获取参数。
: 怎么写多线程呢,一次跑5个url,这样效率会好很多
: ...................
--
FROM 111.16.77.*