出现错误时如何在ThreadPoolExecutor中重新执行功能?

时间:2019-04-01 12:37:23

标签: python multithreading threadpool python-multithreading concurrent.futures

我正在尝试使用python concurrent.futures文档中的example code for python's ThreadPoolExecutor。具体地

import concurrent.futures
import urllib.request

URLS = ['http://www.foxnews.com/',
        'http://www.cnn.com/',
        'http://europe.wsj.com/',
        'http://www.bbc.co.uk/',
        'http://some-made-up-domain.com/']

# Retrieve a single page and report the URL and contents
def load_url(url, timeout):
    with urllib.request.urlopen(url, timeout=timeout) as conn:
        return conn.read()

# We can use a with statement to ensure threads are cleaned up promptly
with concurrent.futures.ThreadPoolExecutor(max_workers=5) as executor:
    # Start the load operations and mark each future with its URL
    future_to_url = {executor.submit(load_url, url, 60): url for url in URLS}
    for future in concurrent.futures.as_completed(future_to_url):
        url = future_to_url[future]
        try:
            data = future.result()
        except Exception as exc:
            print('%r generated an exception: %s' % (url, exc))
        else:
            print('%r page is %d bytes' % (url, len(data)))

如果在concurrent.futures.as_completed(future_to_url)循环中生成异常,如何重新向执行者发送调用?基本上,我只是想重试失败的作业。

我尝试过简单地调用类似executor.submit(...)之类的方法,但这似乎不起作用。

欢迎任何建议,谢谢

1 个答案:

答案 0 :(得分:0)

期货是在失败之后实现的。失败是他们的价值。因此,您无法重新执行它们,必须创建一个新的Future。

有了URL后,您就可以在错误处理程序上进行操作了,.main{ position:relative; border:solid black 1px; width:400px; height:400px; } .item{ border:solid blue 1px; width:160px; height:150px; position: absolute; } .item label{ position:absolute; bottom:5%; left:5%; border:solid red 1px; padding:5px; display:inline-block; font-size:12px; width:min-content; } 中的Futures完成后,可以迭代添加到另一个集合中的新Futures。

相关问题