map用法
代码演示
from concurrent.futures
import ThreadPoolExecutor
def deal_num(n):
return n **
2
if __name__ ==
'__main__':
t = ThreadPoolExecutor()
obj = t.map(deal_num, range(
10))
print(list(obj))
print(
'主')
代码解读
直接用映射map可以简化代码,不需要使用shutdown没有回调函数,直接收集函数运行结果
回调函数
代码演示
from concurrent.futures
import ThreadPoolExecutor
import requests
def get_page(url):
res = requests.get(url).text
return {
'url': url,
'res': res}
def parse_page(res):
res = res.result()
with open(
'a.txt',
'a', encoding=
'utf-8')
as f:
f.write(
'%s-%s\n' % (res[
'url'], len(res[
'res'])))
if __name__ ==
'__main__':
urls = [
'http://www.openstack.org',
'https://www.python.org',
'http://www.sina.com.cn/'
]
t = ThreadPoolExecutor()
for url
in urls:
t.submit(get_page, url).add_done_callback(parse_page)
代码解读
t
.submit(get_page, url)
.add_done_callback(parse_page)
线程池异步提交的任务交给后面的回调函数是将前面产生的future对象给了后面的parse_page函数,所以需要在之前先获取到值。这种方式不需要shutdown