我只是很困惑的一些我写的代码.我很惊讶地发现:
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
results = list(executor.map(f, iterable))
和
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
results = list(map(lambda x: executor.submit(f, x), iterable))
产生不同的结果.第一个生成任何类型f返回的列表,第二个生成一个concurrent.futures.Future对象的列表,然后需要使用result()方法对其进行评估,以获取f返回的值.
我的主要担心是,这意味着executor.map不能利用concurrent.futures.as_completed,这似乎是一种非常方便的方法来评估一些长时间运行的对我正在制作的数据库的调用的结果变得可用
我并不清楚concurrent.futures.ThreadPoolExecutor对象如何工作 – 天真地,我更喜欢(稍微更详细):
with concurrent.futures.ThreadPoolExecutor(max_workers=4) as executor:
result_futures = list(map(lambda x: executor.submit(f, x), iterable))
results = [f.result() for f in futures.as_completed(result_futures)]
在更简洁的executor.map上,以利用可能的性能增益.我错了吗?