简体   繁体   English

迭代未来结果时,如何获取发送到 ThreadPoolExecutor 的 arguments?

[英]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.

相关问题 当线程在 ThreadPoolExecutor() 中死亡时,我如何捕捉? - How can I catch when a thread dies in ThreadPoolExecutor()? 如何在django中预定将来某个时间发送的电子邮件? - How can I schedule an email to be sent at some point in the future in django? 如何使用 ThreadPoolExecutor 测试并发性? - How can I test concurrency using ThreadPoolExecutor? 如何在ThreadPoolExecutor中使用threadlocal变量? - How can I use threadlocal variable with ThreadPoolExecutor? 如何通过python搜索查询来获得Prolog的结果 - how can i get the results of Prolog by searching a query through python 如何循环遍历函数参数? - How can I loop through function arguments? 在迭代每一行时如何维护数据帧的结构(当前将 df 转换为系列)? - How can I maintain a dataframe's structure when iterating through each row(currently converting df to series)? 迭代for循环python时如何检查以前的索引 - How can I check a previous index when iterating through a for loop python 遍历 txt 文件中的行时,如何在正则表达式触发后捕获多个后续行? - When iterating through lines in txt file, how can I capture multiple subsequent lines after a regex triggers? 如何在遍历 for 循环时将 tkinter 变量设置为随机 int 变化 - how can I make a tkinter variable set as random int change when iterating through for loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM