簡體   English   中英

迭代未來結果時,如何獲取發送到 ThreadPoolExecutor 的 arguments?

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

我能想到的一種方法是在返回真/假的基礎上返回我發送的代理,但是有沒有更好的方法可以在外部進行,所以 function 不必只返回一個布爾值?

提交任務時,您可以創建從未來到其代理的映射。

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM