ThreadPoolExecutor获取线程池中已经运行完的任务结果
方法一: 使用as_completed函数
from concurrent.futures import ThreadPoolExecutor, as_completed from time import sleep def method(times): sleep(times) print('sleep {} secondes'.format(times)) return times pool = ThreadPoolExecutor(max_workers=2) seconds = [i for i in range(10)] tasks = [pool.submit(method, (second)) for second in seconds] # as_completed函数会将运行完的任务一个一个yield出来 # 它返回任务的结果与提交任务的顺序无关,谁先执行完返回谁 for future in as_completed(tasks): print(future.result())
方法二: 使用map方法
from concurrent.futures import ThreadPoolExecutor from time import sleep def method(times): sleep(times) print('sleep {} secondes'.format(times)) return times pool = ThreadPoolExecutor(max_workers=2) seconds = [i for i in range(4)] # map方法会将任务运行的结果yield出来 # map方法返回任务结果的顺序与提交任务的顺序一致 for data in pool.map(method, seconds): print(data)
3.wait方法
wait方法用于阻塞主线程的运行, 可以设置当某些子线程运行完成以后再继续执行主线程
其第一个参数是future对象或future对象列表,return_when参数用于指定当哪些任务运行完成时继续执行主线程,其参数有:
FIRST_COMPLETED = 'FIRST_COMPLETED'
FIRST_EXCEPTION = 'FIRST_EXCEPTION'
ALL_COMPLETED = 'ALL_COMPLETED'
其第一个参数是future对象或future对象列表,return_when参数用于指定当哪些任务运行完成时继续执行主线程,其参数有:
FIRST_COMPLETED = 'FIRST_COMPLETED'
FIRST_EXCEPTION = 'FIRST_EXCEPTION'
ALL_COMPLETED = 'ALL_COMPLETED'
from concurrent.futures import ThreadPoolExecutor, wait, ALL_COMPLETED, FIRST_COMPLETED import time # 参数times用来模拟网络请求的时间 def get_html(times): time.sleep(times) print("get page {}s finished".format(times)) return times executor = ThreadPoolExecutor(max_workers=2) urls = [3, 2, 4] # 并不是真的url all_task = [executor.submit(get_html, (url)) for url in urls] wait(all_task, return_when=ALL_COMPLETED) print("main")