Python 如果出现错误,如何在ThreadPoolExecutor中重新执行函数?
我正在python的Python 如果出现错误,如何在ThreadPoolExecutor中重新执行函数?,python,multithreading,threadpool,python-multithreading,concurrent.futures,Python,Multithreading,Threadpool,Python Multithreading,Concurrent.futures,我正在python的concurrent.futures文档中尝试这一点。具体地 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-u
concurrent.futures
文档中尝试这一点。具体地
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\u completed(future\u to\u url)
循环中生成异常,如何向执行器重新发送调用?基本上,我只想重试失败的作业
我尝试过简单地调用类似于executor.submit(…)
的东西,但它似乎不起作用
欢迎提出任何建议,谢谢失败后,未来将实现。失败是他们的价值所在。因此,你不能重新执行它们,你必须创造一个新的未来
由于您有URL,您可以在错误处理程序上执行此操作,并且在
future\u to\u URL
中的期货完成后,迭代添加到不同集合中的新期货。我不确定是否正确理解了它。但是如果我这样做了,如果在另一个集合中出现另一个错误会发生什么呢?这必须是一个循环,它应该以您希望重试的次数发生。好的,我想有比while循环更好的方法来处理这种情况,我将稍等一下,看看其他人是否提出了更优雅的解决方案。谢谢你now@crash我想不是谢谢你写的评论,我永远不会记得这个!