[英]How can I get the arguments I sent to ThreadPoolExecutor when iterating through future results?
I use a ThreadPoolExecutor to quickly check a list of proxies to see which ones are dead or alive.我使用 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?
My goal is to get the argument (the proxy) I passed to the executor when iterating through the results to know which exact proxies are dead or alive, so I could make a dictionary that might look like this:我的目标是在遍历结果时获取传递给执行程序的参数(代理),以了解哪些确切的代理是死的还是活着的,所以我可以制作一个可能看起来像这样的字典:
{"IP1": False, "IP2": True, "IP3": True}
One method I can think of is returning the proxy I sent on top of returning true/false, but is there a better way to do it externally so the function doesn't have to return more than just a bool?我能想到的一种方法是在返回真/假的基础上返回我发送的代理,但是有没有更好的方法可以在外部进行,所以 function 不必只返回一个布尔值?
While submitting the task, you could create a mapping from future to its proxy.提交任务时,您可以创建从未来到其代理的映射。
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())
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.