您可以使用模块
import concurrent.futures
检查此文章的代码和模块样本下载链接 : Contment任务执行在 Python
您可以在未来放置执行结果, 然后得到它们, 这是的样本代码, http://pypi.python.org> :
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://some-made-up-domain.com/ ]
def load_url(url, timeout):
return urllib.request.urlopen(url, timeout=timeout).read()
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
future_to_url = dict((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]
if future.exception() is not None:
print( %r generated an exception: %s % (url,future.exception()))
else:
print( %r page is %d bytes % (url, len(future.result())))