2019独角兽企业重金招聘Python工程师标准>>>
pip install futures
Executor
from concurrent import futures
Executor用来接收异步任务(包含参数),并返回一个Future
Executor
submit(fn, *args, **kwargs)
map(func, *iterables, timeout=None)
shutdown(wait=True)
Executor还有两个子类ThreadPoolExecutor,ProcessPoolExecutor
Future Objects
cancel()
cancelled()
running()
done()
result(timeout=None)
exception(timeout=None)
add_done_callback(fn)
from concurrent import futures
import urllib2
URLS = ['http://www.baidu.com/',
'http://www.google.com/',
'http://my.oschina.net/',
'http://blog.sina.com.cn//']
def load_url(url, timeout):
return urllib2.urlopen(url, timeout=timeout).read()
def main():
with futures.ThreadPoolExecutor(max_workers=5) as executor:
future_to_urls = dict( (executor.submit(load_url, url, 10), url) for url in URLS )
for future in futures.as_completed(future_to_urls):
url = future_to_urls[future]
try:
print '%r page is %d bytes' % (url, len(future.result()))
except Exception as e:
print '%r generated an exception: %s' % (url, e)
if __name__ == '__main__':
main()