遍历未来结果时,如何获取发送给 ThreadPoolExecutor 的参数?

How can I get the arguments I sent to ThreadPoolExecutor when iterating through future results?

我使用 ThreadPoolExecutor 快速检查代理列表,看看哪些是死的还是活的。

with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    futures = []
    for proxy in proxies:
        future = executor.submit(is_proxy_alive, proxy)
        futures.append(future)
    
    for future in futures:
        print(future.result()) # prints true or false depending on if proxy is alive.
                               # how do I get the specific proxy I passed in the arguments 
                               # so that I can make a dictionary here?

我的目标是在遍历结果时获取我传递给执行程序的参数(代理)以了解哪些确切的代理是死的还是活的,所以我可以制作一个看起来像这样的字典:

{"IP1": False, "IP2": True, "IP3": True}

我能想到的一种方法是 returning 我在 returning true/false 之上发送的代理,但是有没有更好的方法在外部执行此功能不需要 return 不仅仅是一个布尔值?

在提交任务时,您可以创建一个从 future 到它的代理的映射。

with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    future_proxy_mapping = {} 
    futures = []
    for proxy in proxies:
        future = executor.submit(is_proxy_alive, proxy)
        future_proxy_mapping[future] = proxy
        futures.append(future)
    
    for future in futures:
        proxy = future_proxy_mapping[future]
        print(proxy)
        print(future.result())