python threadpoolexecutor_python – ThreadPoolExecutor().map与ThreadPoolExecutor()的不同.submit?...

我只是很困惑的一些我写的代码.我很惊讶地发现:

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上,以利用可能的性能增益.我错了吗?

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值