I am a first time user of concurrent.futures and following the official guides.
Problem: Inside the as_completed() block, how do I access the k, v which is inside the future_to_url?
The k variable is vital.
Using something like:
for (future, k,v) in zip(concurrent.futures.as_completed(future_to_url), urls.items()):
I stumbled on this post however I cannot decipher the syntax to reproduce
Original
def start():
with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
future_to_url = {executor.submit(visit_url, v): v for k, v in urls.items()}
for future in concurrent.futures.as_completed(future_to_url):
data = future.result()
json = data.json()
print(f"k: {future[k]}")
Second Attempt - Using zip which breaks
def start():
with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
future_to_url = {executor.submit(visit_url, v): v for k, v in urls.items()}
for (future, k, v) in zip(concurrent.futures.as_completed(future_to_url), urls.items()):
data = future.result()
json = data.json()
print(f"k: {k}")
Third Broken Attempt - Using Map source
for future, (k, v) in map(concurrent.futures.as_completed(future_to_url), scraping_robot_urls.items()):
TypeError: 'generator' object is not callable
Fourth Broken Attempt - Storing the k,v pairs before the as_completed() loop and pairing them with an enumerate index
with concurrent.futures.ThreadPoolExecutor(max_workers=50) as executor:
future_to_url = {executor.submit(get_response, v): v for k, v in scraping_robot_urls.items()}
info = {k: v for k, v in scraping_robot_urls.items()}
for i, future in enumerate(concurrent.futures.as_completed(future_to_url)):
url = future_to_url[future]
data = future.result()
print(f"data: {data}")
print(f"key: {list(info)[i]} / url: {url}")
This does not work as the URL, does not match the key, they seem to be mismatched, and I cannot rely on this behaviour working.
For completeness, here are the dependencies
def visit_url(url):
return requests.get(url)
urls = {
'id123': 'www.google.com',
'id456': 'www.bing.com',
'id789': 'www.yahoo.com'
}
Sources of inspiration: