Python线程池
ThreadPoolExecutor 使用语法
from cuncurrent.futures import ThreadPoolExecutor,as_completed
-
用法1(注意map的结果和入参是顺序对应的)
-
简单
-
with ThreadPoolExecutor() as pool:
results = pool.map(craw,urls) #craw:函数名 urls:参数列表(多个)
for result in results:
print(result)
-
用法2(注意如果用as_completed顺序是不定的)
-
更强大(执行完哪个就返回哪个)
-
with ThreadPoolExecutor() as pool:
futures = [pool.submit(craw,url) for url in urls]
for future in futures:
print(future.result())
for future in as_completed(futures):
print(future.result())