我有一个要通过 concurrent.futures 的 ThreadPoolExecutor 下载的 url 列表,但可能有一些超时 url,我想在所有第一次尝试结束后重新下载它们。我不知道该怎么做,这是我的尝试,但因无休止的打印“time_out_again”而失败:
import concurrent.futures
def player_url(url):
# here. if timeout, return 1. otherwise do I/O and return 0.
...
urls = [...]
time_out_futures = [] #list to accumulate timeout urls
with concurrent.futures.ThreadPoolExecutor(max_workers=10) as executor:
future_to_url = (executor.submit(player_url, url) for url in urls)
for future in concurrent.futures.as_completed(future_to_url):
if future.result() == 1:
time_out_futures.append(future)
# here is what I try to deal with all the timeout urls
while time_out_futures:
future = time_out_futures.pop()
if future.result() == 1:
print('time_out_again')
time_out_futures.insert(0,future) # add back to the list
那么,有没有办法解决这个问题呢?